一、Memcache安装
下载地址:https://github.com/memcached/memcached/releases
官方wiki:https://github.com/memcached/memcached/wiki
启动参数:
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
二、pom文件
1 <!-- memcache --> 2 <dependency> 3 <groupId>com.whalin</groupId> 4 <artifactId>Memcached-Java-Client</artifactId> 5 <version>3.0.2</version> 6 </dependency>
三、配置文件
memcache.properties 文件
1 #设置服务器地址.该端口号默认为11211 2 memcached.server=192.168.208.131:11211 3 #容错 4 memcached.failOver=true 5 #设置初始连接数 6 memcached.initConn=20 7 #设置最小连接数 8 memcached.minConn=10 9 #设置最大连接数 10 memcached.maxConn=250 11 #设置连接池维护线程的睡眠时间 12 memcached.maintSleep=3000 13 #设置是否使用Nagle算法(Socket的参数),如果是true在写数据时不缓冲,立即发送出去 14 memcached.nagle=false 15 #设置socket的读取等待超时时间 16 memcached.socketTO=3000 17 #设置连接心跳监测开关 18 memcached.aliveCheck=true
memcache.xml 文件
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/spring-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.xsd 10 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.PropertyPlaceholderConfigurer"> 13 <property name="locations"> 14 <list> 15 <value>classpath:memcache.properties</value> 16 </list> 17 </property> 18 </bean> 19 <!-- Memcached配置 --> 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>
SockIOPool、MemCachedClient构造器参数poolName需要保持一直,否则会报 attempting to get SockIO from uninitialized pool! 四、测试用例
1 /** 2 * Mecached测试类 3 * 4 */ 5 @RunWith(SpringJUnit4ClassRunner.class) 6 @ContextConfiguration(locations = "classpath:application.xml") 7 public class MemcacheTest { 8 9 @Autowired 10 private MemCachedClient memCachedClient; 11 12 @Test 13 public void memCachedGetTest() { 14 memCachedClient.set("hello", "123", new Date(System.currentTimeMillis() + 10000L)); 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 }
时间: 2024-10-26 00:35:11