在測試項目中引入了memcached做爲緩存層,如下是memcached的緩存配置和調用過程。
html
直接參考之前的博文 linux下安裝memcached過程 再也不詳細介紹java
在2.6及之前版本使用的是java_memcached-release_2.6.3.jar,3.0以上版本使用memcached-Java-Client-3.0.2.jar,下載地址爲http://mvnrepository.com/artifact/com.whalin/Memcached-Java-Client。這兩個版本差別較大,官方說法是因爲danga的域名不屬於他們全部,因此如今修改了類包名稱。這是官方的wiki有相關的解釋:https://github.com/gwhalin/Memcached-Java-Client/wikilinux
memcached-Java-Client-3.0.2.jar 須要如下一些包,git
一、commons-pool2-2.4.1.jar依賴包,下載地址http://mvnrepository.com/artifact/org.apache.commons/commons-pool2github
二、junitapache
三、slf4j
緩存
<bean id="memcachedPool" class="com.schooner.MemCached.SchoonerSockIOPool" factory-method="getInstance" init-method="initialize"> <constructor-arg> <value>neeaMemcachedPool</value> </constructor-arg> <property name="servers"> <list> <value>memcached安裝的服務器ip地址:memcached的監聽端口</value> </list> </property> <property name="initConn"> <value>20</value> </property> <property name="minConn"> <value>10</value> </property> <property name="maxConn"> <value>50</value> </property> <property name="nagle"> <value>false</value> </property> <property name="socketTO"> <value>3000</value> </property> </bean> <bean id="memcachedClient" class="com.whalin.MemCached.MemCachedClient"> <constructor-arg> <value>neeaMemcachedPool</value> </constructor-arg> </bean>
public class Memcached { static MemCachedClient cachedClient; private Memcached(){ } public static void init() { ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:setup/applicationContext.xml"); cachedClient = (MemCachedClient)ctx.getBean("memcachedClient"); } public static Object get(String key) { return cachedClient.get(key); } public static boolean add(String key, Object value) { return cachedClient.add(key, value); } public static boolean add(String key, Object value, Integer expire) { return cachedClient.add(key, value, expire); } public static boolean set(String key, Object value) { return cachedClient.set(key, value); } public static boolean set(String key, Object value, Integer expire) { return cachedClient.set(key, value, expire); } public static boolean replace(String key, Object value) { return cachedClient.replace(key, value); } public static boolean replace(String key, Object value, Integer expire) { return cachedClient.replace(key, value, expire); } public static boolean delete(String key){ return cachedClient.delete(key); } }
Memcached.set("memcached", "測試"); System.out.println("memcached緩存————————="+ Memcached.get("memcached"));