您的位置:首页 > 数据库 > Memcache

Spring Memcache配置及缺少com.danga的解决

2016-07-05 09:06 453 查看
需要以下jar



但是maven配置,会无法下载对应jar,主要是由于maven repository中没有,我们需要从

https://github.com/gwhalin/Memcached-Java-Client/downloads

中下载对应版本,手动导入本地maven仓库

<dependency>
<groupId>com.danga</groupId>
<artifactId>java-memcached</artifactId>
<version>2.6.6</version>
</dependency>


手动导入maven仓库的命令

mvn install:install-file -Dfile=D:/java_memcached-release_2.6.6.jar -DgroupId=com.danga
-DartifactId=java-memcached  -Dversion=2.6.6 -Dpackaging=jar


这样我们仓库中就有这个jar包了,然后再导入其他三个jar就行

spring-memcache.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:com/ys/memcache/memcache.properties</value>
</list>
</property>
</bean>

<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<value>neeaMemcachedPool</value>
</constructor-arg>
<!-- 可以配置多服务器负载均衡 -->
<property name="servers">
<list>
<value>${memcache.server}</value>
</list>
</property>
<!-- 初始连接数 -->
<property name="initConn">
<value>${memcache.initConn}</value>
</property>
<!-- 最小连接数 -->
<property name="minConn">
<value>${memcache.minConn}</value>
</property>
<!-- 最大连接数 -->
<property name="maxConn">
<value>${memcache.maxConn}</value>
</property>
<!-- 连接池守护线程的睡眠时间 -->
<property name="maintSleep">
<value>${memcache.maintSleep}</value>
</property>
<property name="nagle">
<value>${memcache.nagle}</value>
</property>
<property name="socketTO">
<value>${memcache.socketTO}</value>
</property>
</bean>

<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
<constructor-arg>
<value>neeaMemcachedPool</value>
</constructor-arg>
</bean>
</beans>


memcache.properties

memcache.server=127.0.0.1:11211
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000


测试类代码SpringMemcache.java

public class SpringMemcache {

MemCachedClient memCachedClient;

@Before
public void beforeTest() {

ApplicationContext atx = new ClassPathXmlApplicationContext("com/ys/memcache/spring-memcache.xml");
memCachedClient = (MemCachedClient) atx.getBean("memcachedClient");
}

@Test
public void TestMem() {
memCachedClient.set("name", "han");
System.out.println(memCachedClient.get("name"));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring maven jar