java客户端包文件:
java_memcached-release_2.6.6.zip
添加配置文件:memcached.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
然后在sping的配置文件中添加bean
首先将原来的jdbc配置文件地址修改一下:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:memcached.properties</value>
</list>
</property>
<!-- <property name="location" value="classpath:jdbc.properties" /> -->
</bean>
然后再添加:
<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
factory-method="getInstance" init-method="initialize"
destroy-method="shutDown">
<constructor-arg>
<value>memCachedPool</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>memCachedPool</value>
</constructor-arg>
</bean>
配置完毕,然后测试:
@Resource
private MemCachedClient memCachedClient;
@RequestMapping("/test")
public void test(){
Boolean isSuccess=memCachedClient.set("yxxrui", "www.yxxrui.cn");
logger.info("加载是否成功:"+isSuccess);
String result =(String)memCachedClient.get("yxxrui");
logger.info("获取的结果为:"+result);
}
原创文章如转载,请注明出处“
伊人博客”