Spring整合Memcache執行個體,springmemcache執行個體
一、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=25011 #設定串連池維護線程的睡眠時間12 memcached.maintSleep=300013 #設定是否使用Nagle演算法(Socket的參數),如果是true在寫資料時不緩衝,立即發送出去14 memcached.nagle=false15 #設定socket的讀取等待逾時時間16 memcached.socketTO=300017 #設定串連心跳監測開關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.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.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 @Autowired10 private MemCachedClient memCachedClient;11 12 @Test13 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 }