Spring集成memcached的详细介绍
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring集成memcached的详细介绍相关的知识,希望对你有一定的参考价值。
前提条件:工程需要引入jar包java_memcached-release_2.0.1.jar
第一步:添加memcached的配置文件。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreResourceNotFound" value="false" /> <property name="locations"> <list> <value>classpath:memcache.properties</value> </list> </property> </bean>
配置文件内容如下:
memcache.server=xxx.xxx.xxx.xxx:11111
memcache.weights=1
memcache.initConn=1
memcache.minConn=1
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000
第二步:添加memcached的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="weights"><list><value>${memcache.weights}</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>
下面看一下com.danga.MemCached.SockIOPool的源代码,重点是SockIOPool构造函数:
public static synchronized SockIOPool getInstance(String poolName) { if (pools.containsKey(poolName)) return pools.get(poolName); SockIOPool pool = new SockIOPool(); pools.put(poolName, pool); return pool; }
<bean id="memCacheClient" class="com.danga.MemCached.MemCachedClient"> <constructor-arg><value>memCachedPool</value></constructor-arg> </bean>
下面看一下com.danga.MemCached.MemCachedClient的源代码,重点是MemCachedClient的构造函数:
public MemCachedClient(String poolName) { this.poolName = poolName; init(); }
第三步:测试memcached的功能。
public class MemcacheTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); MemCachedClient memCachedClient=(MemCachedClient)context.getBean("memCacheClient"); memCachedClient.set("hello", "swiftlet"); memCachedClient.get("hello"); } }
以上是关于Spring集成memcached的详细介绍的主要内容,如果未能解决你的问题,请参考以下文章