Spring integrates Memcache instances and springmemcache instances
1. Install Memcache
: Https://github.com/memcached/memcached/releases
Official wiki: https://github.com/memcached/memcached/wiki
Startup parameters:
-P listening port
-L connected IP address. The default value is local
-D start: start the memcached service.
-D restart: restart the memcached service.
-D stop | shutdown the running memcached Service
-D install the memcached Service
-D uninstall memcached Service
-U runs as the identity (only valid when running as root)
-MB maximum memory usage, in MB. The default value is 64 MB.
-An error is returned when M memory is used up, instead of deleting items.
-C: Maximum number of simultaneous connections. The default value is 1024.
-F block size growth factor, default value: 1.25
-N: minimum allocation space. The default value of key + value + flags is 48.
-H Show Help
Ii. pom File
1 <!-- memcache -->2 <dependency>3 <groupId>com.whalin</groupId>4 <artifactId>Memcached-Java-Client</artifactId>5 <version>3.0.2</version>6 </dependency>
3. Configuration File
Memcache. properties File
1 # Set the server address. this port number is 11211 2 memcached by default. server = 192.168.208.131: 11211 3 # Fault Tolerance 4 memcached. failOver = true 5 # Set the initial number of connections 6 memcached. initConn = 20 7 # set the minimum number of connections 8 memcached. minConn = 10 9 # set the maximum number of connections to 10 memcached. maxConn = 25011 # Set the sleep time of the connection pool maintenance thread 12 memcached. maintSleep = 300013 # Set whether to use the Nagle algorithm (Socket parameter). If it is true, the system will immediately send out 14 memcached if data is not buffered during data writing. nagle = false15 # Set the socket read wait timeout time 16 memcached. socketTO = 300017 # Set the connection heartbeat monitoring switch 18 memcached. aliveCheck = true
Memcache. xml file
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" 4 xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" 5 xsi: schemaLocation = "http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/s Pring-beans-2.5.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-2.5.xsd 9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd> 11 12 <bean class = "org. springframework. beans. factory. Config. propertyplaceholderpolicer "> 13 <property name =" locations "> 14 <list> 15 <value> classpath: memcache. properties </value> 16 </list> 17 </property> 18 </bean> 19 <! -- Memcached configuration --> 20 <bean id = "memcachedPool" class = "com. whalin. memCached. sockIOPool "21 factory-method =" getInstance "init-method =" initialize "destroy-method =" shutDown "> 22 <constructor-arg> 23 <value> memCachedPool </value> 24 </constructor-arg> 25 <property name = "servers"> 26 <list> 27 <value >$ {memcached. server} </value> 28 </list> 29 </property> 30 <property name = "initConn"> 31 <value >$ {memcached. initConn} </value> 32 </property> 33 <property name = "minConn"> 34 <value >$ {memcached. minConn} </value> 35 </property> 36 <property name = "maxConn"> 37 <value >$ {memcached. maxConn} </value> 38 </property> 39 <property name = "maintSleep"> 40 <value >$ {memcached. maintSleep} </value> 41 </property> 42 <property name = "nagle"> 43 <value >$ {memcached. nagle} </value> 44 </property> 45 <property name = "socketTO"> 46 <value >$ {memcached. socketTO} </value> 47 </property> 48 </bean> 49 50 <bean id = "memCachedClient" class = "com. whalin. memCached. memCachedClient "> 51 <constructor-arg> 52 <value> memCachedPool </value> 53 </constructor-arg> 54 </bean> 55 </beans>
The poolName parameter of the SockIOPool and MemCachedClient constructors must be retained. Otherwise, attempting to get SockIO from uninitialized pool will be reported!
Iv. Test Cases
1/** 2 * Mecached test Class 3*4 */5 @ RunWith (SpringJUnit4ClassRunner. class) 6 @ ContextConfiguration (locations = "classpath: application. xml ") 7 public class MemcacheTest {8 9 @ Autowired10 private MemCachedClient memCachedClient; 11 12 @ Test13 public void memCachedGetTest () {14 memCachedClient. set ("hello", "123", new Date (System. currentTimeMillis () + yml); 15 16 System. out. println ("hello:" + memCachedClient. get ("hello"); 17 18 try {19 Thread. sleep (11000); 20} catch (Exception e) {} 21 Assert. assertNull (memCachedClient. get ("hello"); 22 23} 24}