Spring集成memcached的详细介绍
提条件:工程需要引入jar包java_memcached-release_2.0.1.jar
第一步:添加memcached的配置文件。
1 2 3 4 5 6 7 8 9 10 | <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <propertyname="systemPropertiesModeName"value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> <propertyname="ignoreResourceNotFound"value="false"/> <propertyname="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管理。
1 2 3 4 5 6 7 8 9 10 11 12 | <bean id="memcachedPool"class="com.danga.MemCached.SockIOPool"factory-method="getInstance" init-method="initialize"destroy-method="shutDown"> <constructor-arg><value>memCachedPool</value></constructor-arg> <propertyname="servers"><list><value>${memcache.server}</value></list></property> <propertyname="weights"><list><value>${memcache.weights}</value></list></property> <propertyname="initConn"><value>${memcache.initConn}</value></property> <propertyname="minConn"><value>${memcache.minConn}</value></property> <propertyname="maxConn"><value>${memcache.maxConn}</value></property> <propertyname="maintSleep"><value>${memcache.maintSleep}</value></property> <propertyname="nagle"><value>${memcache.nagle}</value></property> <propertyname="socketTO"><value>${memcache.socketTO}</value></property> </bean> |
下面看一下com.danga.MemCached.SockIOPool的源代码,重点是SockIOPool构造函数:
1 2 3 4 5 6 7 8 9 10 | publicstaticsynchronized SockIOPool getInstance(StringpoolName) { if(pools.containsKey(poolName)) returnpools.get(poolName); SockIOPool pool=newSockIOPool(); pools.put(poolName,pool); returnpool; } |
1 2 3 | <bean id="memCacheClient"class="com.danga.MemCached.MemCachedClient"> <constructor-arg><value>memCachedPool</value></constructor-arg> </bean> |
下面看一下com.danga.MemCached.MemCachedClient的源代码,重点是MemCachedClient的构造函数:
1 2 3 4 5 | publicMemCachedClient(StringpoolName) { this.poolName=poolName; init(); } |
第三步:测试memcached的功能。
1 2 3 4 5 6 7 8 9 | publicclassMemcacheTest{ publicstaticvoidmain(String[]args) { ApplicationContext context=newClassPathXmlApplicationContext("applicationContext.xml"); MemCachedClient memCachedClient=(MemCachedClient)context.getBean("memCacheClient"); memCachedClient.set("hello","swiftlet"); memCachedClient.get("hello"); } } |
声明: 本文由金丝燕网原创编译,转载请保留链接: Spring集成memcached的详细介绍
相关推荐
郗瑞强 2020-08-16
85590296 2020-07-22
jkzyx 2020-06-29
luotuo 2020-06-26
LinuxJob 2020-06-26
ol0 2020-06-26
清溪算法君老号 2020-06-25
86251043 2020-06-13
CSDN0BLOG 2020-06-09
ol0 2020-05-26
andyhuabing 2020-05-22
程序员俱乐部 2020-05-06
83530391 2020-05-05
ol0 2020-05-02
83530391 2020-04-09
85590296 2020-03-25
carolAnn 2020-03-07
大脸猫脸大 2020-03-03
ol0 2020-02-18