當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring集成Memcached三种方式(一)
生活随笔
收集整理的這篇文章主要介紹了
Spring集成Memcached三种方式(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring集成Memcached三種方式(一)
轉載:http://blog.csdn.net/u013725455/article/details/52102170
?
Memcached Client目前有3種:?
Memcached Client for Java?
SpyMemcached?
XMemcached
這三種Client一直存在各種爭議:?
Memcached Client for Java 比 SpyMemcached更穩定、更早、更廣泛;?
SpyMemcached 比 Memcached Client for Java更高效;?
XMemcached 比 SpyMemcache并發效果更好。
方式一:Memcached Client for Java?
jar包準備:java_memcached-release_2.6.6.jar
memcached.properties配置文件:配置服務器地址,連接數,超時時間等
?
#######################Memcached配置####################### #服務器地址 memcached.server1=127.0.0.1 memcached.port1=11211 #memcached.server=127.0.0.1:11211 #初始化時對每個服務器建立的連接數目 memcached.initConn=20 #每個服務器建立最小的連接數 memcached.minConn=10 #每個服務器建立最大的連接數 memcached.maxConn=50 #自查線程周期進行工作,其每次休眠時間 memcached.maintSleep=3000 #Socket的參數,如果是true在寫數據時不緩沖,立即發送出去 memcached.nagle=false #Socket阻塞讀取數據的超時時間 memcached.socketTO=3000 ##pool.setServers(servers); ##pool.setWeights(weights); ##pool.setHashingAlg(SockIOPool.CONSISTENT_HASH); ##pool.setInitConn(5); ##pool.setMinConn(5); ##pool.setMaxConn(250); ##pool.setMaxIdle(1000 * 60 * 60 * 6); ##pool.setMaintSleep(30); ##pool.setNagle(false); ##pool.setSocketTO(3000); ##pool.setSocketConnectTO(0);?
配置Bean
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "><!-- properties config --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="1"/> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="locations"> <list> <!--<value>classpath:/com/springmvc/config/memcached.properties</value>--> <value>/WEB-INF/config/memcached.properties</value> </list> </property> </bean> <!-- Memcached配置 --> <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown"> <property name="servers"> <!-- ${memcached.server} --><list> <value>${memcached.server1}:${memcached.port1}</value> </list> </property> <property name="initConn"> <value>${memcached.initConn}</value> </property> <property name="minConn"> <value>${memcached.minConn}</value> </property> <property name="maxConn"> <value>${memcached.maxConn}</value> </property> <property name="maintSleep"> <value>${memcached.maintSleep}</value> </property> <property name="nagle"> <value>${memcached.nagle}</value> </property> <property name="socketTO"> <value>${memcached.socketTO}</value> </property> </bean> </beans>將app-context-memcached.xml配置到app-context.xml中
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "><import resource="app-context-memcached.xml" /><!--memcached 緩存配置 --><!-- <import resource="app-context-xmemcached.xml" /> --><!-- <import resource="app-context-spymemcached.xml" /> --><!-- @Component and @Resource --><beanclass="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /><!-- 對com包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 --><context:component-scan base-package="com.springmvc.imooc" /><!-- 定時器 --><!-- <task:annotation-driven /> --><!-- mvc --><mvc:annotation-driven /><!-- Aspect --><!-- <aop:aspectj-autoproxy /> --> </beans>Memcached工具類:
/** 文件名:MemcachedUtils.java* 版權:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. * 描述: MemcachedUtils.java* 修改人:peiyu* 修改時間:2016年8月2日* 修改內容:新增*/ package com.springmvc.imooc.util;import java.io.PrintWriter; import java.io.StringWriter; import java.util.Date;import com.danga.MemCached.MemCachedClient;/*** @author peiyu*/ public final class MemcachedUtils {/*** cachedClient.*/private static MemCachedClient cachedClient;static {if (cachedClient == null) {cachedClient = new MemCachedClient("memcachedPool");}}/*** 構造函數.*/private MemcachedUtils() {}/*** 向緩存添加新的鍵值對。如果鍵已經存在,則之前的值將被替換.* @param key 鍵* @param value 值* @return boolean*/public static boolean set(String key, Object value) {return setExp(key, value, null);}/*** 向緩存添加新的鍵值對。如果鍵已經存在,則之前的值將被替換.* @param key 鍵* @param value 值* @param expire 過期時間 New Date(1000*10):十秒后過期* @return boolean*/public static boolean set(String key, Object value, Date expire) {return setExp(key, value, expire);}/*** 向緩存添加新的鍵值對。如果鍵已經存在,則之前的值將被替換.* @param key 鍵* @param value 值* @param expire 過期時間 New Date(1000*10):十秒后過期* @return boolean*/private static boolean setExp(String key, Object value, Date expire) {boolean flag = false;try {flag = cachedClient.set(key, value, expire);} catch (Exception e) {MemcachedLog.writeLog("Memcached set方法報錯,key值:" + key + "\r\n" + exceptionWrite(e));}return flag;}/*** 僅當緩存中不存在鍵時,add 命令才會向緩存中添加一個鍵值對.* @param key 鍵* @param value 值* @return boolean*/public static boolean add(String key, Object value) {return addExp(key, value, null);}/*** 僅當緩存中不存在鍵時,add 命令才會向緩存中添加一個鍵值對.* @param key 鍵* @param value 值* @param expire 過期時間 New Date(1000*10):十秒后過期* @return boolean*/public static boolean add(String key, Object value, Date expire) {return addExp(key, value, expire);}/*** 僅當緩存中不存在鍵時,add 命令才會向緩存中添加一個鍵值對.* @param key 鍵* @param value 值* @param expire 過期時間 New Date(1000*10):十秒后過期* @return boolean*/private static boolean addExp(String key, Object value, Date expire) {boolean flag = false;try {flag = cachedClient.add(key, value, expire);} catch (Exception e) {MemcachedLog.writeLog("Memcached add方法報錯,key值:" + key + "\r\n" + exceptionWrite(e));}return flag;}/*** 僅當鍵已經存在時,replace 命令才會替換緩存中的鍵.* @param key 鍵* @param value 值* @return boolean*/public static boolean replace(String key, Object value) {return replaceExp(key, value, null);}/*** 僅當鍵已經存在時,replace 命令才會替換緩存中的鍵.* @param key 鍵* @param value 值* @param expire 過期時間 New Date(1000*10):十秒后過期* @return boolean*/public static boolean replace(String key, Object value, Date expire) {return replaceExp(key, value, expire);}/*** 僅當鍵已經存在時,replace 命令才會替換緩存中的鍵.* @param key 鍵* @param value 值* @param expire 過期時間 New Date(1000*10):十秒后過期* @return boolean*/private static boolean replaceExp(String key, Object value, Date expire) {boolean flag = false;try {flag = cachedClient.replace(key, value, expire);} catch (Exception e) {MemcachedLog.writeLog("Memcached replace方法報錯,key值:" + key + "\r\n" + exceptionWrite(e));}return flag;}/*** get 命令用于檢索與之前添加的鍵值對相關的值.* @param key 鍵* @return boolean*/public static Object get(String key) {Object obj = null;try {obj = cachedClient.get(key);} catch (Exception e) {MemcachedLog.writeLog("Memcached get方法報錯,key值:" + key + "\r\n" + exceptionWrite(e));}return obj;}/*** 刪除 memcached 中的任何現有值.* @param key 鍵* @return boolean*/public static boolean delete(String key) {return deleteExp(key, null);}/*** 刪除 memcached 中的任何現有值.* @param key 鍵* @param expire 過期時間 New Date(1000*10):十秒后過期* @return boolean*/public static boolean delete(String key, Date expire) {return deleteExp(key, expire);}/*** 刪除 memcached 中的任何現有值.* @param key 鍵* @param expire 過期時間 New Date(1000*10):十秒后過期* @return boolean*/@SuppressWarnings("deprecation")private static boolean deleteExp(String key, Date expire) {boolean flag = false;try {flag = cachedClient.delete(key, expire);} catch (Exception e) {MemcachedLog.writeLog("Memcached delete方法報錯,key值:" + key + "\r\n" + exceptionWrite(e));}return flag;}/*** 清理緩存中的所有鍵/值對.* @return boolean*/public static boolean flashAll() {boolean flag = false;try {flag = cachedClient.flushAll();} catch (Exception e) {MemcachedLog.writeLog("Memcached flashAll方法報錯\r\n" + exceptionWrite(e));}return flag;}/*** 返回異常棧信息,String類型.* @param e Exception* @return boolean*/private static String exceptionWrite(Exception e) {StringWriter sw = new StringWriter();PrintWriter pw = new PrintWriter(sw);e.printStackTrace(pw);pw.flush();return sw.toString();} }
測試:
System.out.println(MemcachedUtils.set("aa", "bb", new Date(1000 * 60))); Object obj = MemcachedUtils.get("aa"); System.out.println("***************************"); System.out.println(obj.toString());?
posted on 2016-08-03 18:16 舞飛林 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/rench/p/5733964.html
總結
以上是生活随笔為你收集整理的Spring集成Memcached三种方式(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux中环境变量文件及配置
- 下一篇: JAVA对象JSON数据互相转换的四种常