Memcached的配置,SSH项目中的整合(com.whalin),Memcached工具类,Memcached的代码调用
1 修改pom.xml,添加依賴文件:
| <dependency> ??? <groupId>com.whalin</groupId> ??? <artifactId>Memcached-Java-Client</artifactId> ?? ?<version>3.0.2</version> </dependency> |
2 添加memcached-context.xml,注意要在web.xml中進行配置
| <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" ??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" ??? xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" ??? xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" ??? xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ?????? http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ?????? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ?????? http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd ?????? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> ??? ??? <!—注意下面的:memcache在使用的時候會用到--> ??? <beans:bean id="memcache" class="com.whalin.MemCached.SockIOPool" ?????? factory-method="getInstance" init-method="initialize" destroy-method="shutDown"> ?????? <beans:constructor-arg> ?????????? <beans:value>memcache</beans:value> ?????? </beans:constructor-arg> ?????? <beans:property name="servers"> ?????????? <beans:list> ???????????????? <!--服務器地址--> ????????????? <beans:value>172.16.24.27:11211</beans:value> ?????????? </beans:list> ?????? </beans:property> ???????? <!--初始化時對每個服務器建立的連接數目--> ?????? <beans:property name="initConn"> ?????????? <beans:value>20</beans:value> ?????? </beans:property> ???????? <!--每個服務器建立最小的連接數--> ?????? <beans:property name="minConn"> ?????????? <beans:value>10</beans:value> ?????? </beans:property> ???????? <!--每個服務器建立最大的連接數--> ?????? <beans:property name="maxConn"> ?????????? <beans:value>50</beans:value> ?????? </beans:property> ???????? <!--自查線程周期進行工作,其每次休眠時間--> ?????? <beans:property name="maintSleep"> ?????????? <beans:value>1000</beans:value> ?????? </beans:property> ???????? <!--Socket的參數,如果是true在寫數據時不緩沖,立即發送出去--> ?????? <beans:property name="nagle"> ?????????? <beans:value>false</beans:value> ?????? </beans:property> ???????? <!--Socket阻塞讀取數據的超時時間--> ?????? <beans:property name="socketTO"> ?????????? <beans:value>1000</beans:value> ?????? </beans:property> ???????? <!-- 當memcached的連接路徑出現問題的時候,代碼連接的時候時間超時設置 --> ?????? <beans:property name="socketConnectTO"> ?????????? <beans:value>500</beans:value> ?????? </beans:property> ??? </beans:bean> </beans:beans> |
3 在web.xml中配置:
4 編寫MemcachedUtils,代碼如下:
| package com.kuman.cartoon.utils;? ? import java.util.Date; ? import org.apache.log4j.Logger; ? import com.whalin.MemCached.MemCachedClient; ? ? /** ?* @ClassName: MemcachedUtils ?* @Description: Memcached工具類 ?* @author ?* @date 2015-8-6 ?*? ?*/ public class MemcachedUtils { ??? private static final Logger logger = Logger.getLogger(MemcachedUtils.class);? ??? private static MemCachedClient cachedClient; ??? static {? ??????? if (cachedClient == null) ??????????? //括號中的名稱要和配置文件memcached-context.xml中的名稱一致 ??????????? cachedClient = new MemCachedClient("memcache"); ??? }? ? ??? private MemcachedUtils() {}? ? ??? /** ???? * 向緩存添加新的鍵值對。如果鍵已經存在,則之前的值將被替換。 ???? *? ???? * @param key ???? *??????????? 鍵 ???? * @param value ???? *??????????? 值 ???? * @return ???? */? ??? public static boolean set(String key, Object value) {? ??????? return setExp(key, value, null);? ??? }? ? ??? /** ???? * 向緩存添加新的鍵值對。如果鍵已經存在,則之前的值將被替換。 ???? *? ???? * @param key ???? *??????????? 鍵 ???? * @param value ???? *??????????? 值 ??? ?* @param expire ???? *??????????? 過期時間 New Date(1000*10):十秒后過期 ???? * @return ???? */? ??? 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 ???? */? ??? private static boolean setExp(String key, Object value, Date expire) {? ??????? boolean flag = false;? ??????? try {? ??????????? flag = cachedClient.set(key, value, expire);? ??????? } catch (Exception e) {? ??????????? // 記錄Memcached日志? ?????? ????????? logger.error("Memcached set方法報錯,key值:" + key + "\r\n");? ??????? }? ??????? return flag;? ??? }? ? ??? /** ???? * 僅當緩存中不存在鍵時,add 命令才會向緩存中添加一個鍵值對。 ???? *? ???? * @param key ???? *??????????? 鍵 ???? * @param value ???? *??????????? 值 ???? * @return ???? */? ??? 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 ???? */? ??? 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 ???? */? ??? private static boolean addExp(String key, Object value, Date expire) {? ??????? boolean flag = false;? ??????? try {? ??????????? flag = cachedClient.add(key, value, expire);? ??????? } catch (Exception e) {? ??????????? // 記錄Memcached日志? ??????????? logger.error("Memcached add方法報錯,key值:" + key + "\r\n");? ??????? }? ??????? return flag;? ??? }? ? ??? /** ???? * 僅當鍵已經存在時,replace 命令才會替換緩存中的鍵。 ???? *? ???? * @param key ???? *??????????? 鍵 ???? * @param value ???? *??????????? 值 ? ???* @return ???? */? ??? 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 ???? */? ??? 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 ???? */? ??? private static boolean replaceExp(String key, Object value, Date expire) {? ??????? boolean flag = false;? ??????? try {? ??????????? flag = cachedClient.replace(key, value, expire);? ??????? } catch (Exception e) {? ??????????? logger.error("Memcached replace方法報錯,key值:" + key + "\r\n");? ??????? }? ??????? return flag;? ??? }? ? ??? /** ???? * get 命令用于檢索與之前添加的鍵值對相關的值。 ???? *? ???? * @param key ???? *??????????? 鍵 ???? * @return ???? */? ??? public static Object get(String key) {? ??????? Object obj = null;? ??????? try {? ??????????? obj = cachedClient.get(key);? ??????? } catch (Exception e) {? ??????????? logger.error("Memcached get方法報錯,key值:" + key + "\r\n");? ??????? }? ??????? return obj;? ??? }? ? ??? /** ???? * 刪除 memcached 中的任何現有值。 ???? *? ???? * @param key ???? *??????????? 鍵 ???? * @return ???? */? ??? public static boolean delete(String key) {? ??????? return deleteExp(key, null);? ??? }? ? ??? /** ???? * 刪除 memcached 中的任何現有值。 ???? *? ???? * @param key ???? *??????????? 鍵 ???? * @param expire ???? *??????????? 過期時間 New Date(1000*10):十秒后過期 ???? * @return ???? */? ??? public static boolean delete(String key, Date expire) {? ??????? return deleteExp(key, expire);? ??? }? ? ??? /** ???? * 刪除 memcached 中的任何現有值。 ???? *? ???? * @param key ???? *??????????? 鍵 ???? * @param expire ???? *??????????? 過期時間 New Date(1000*10):十秒后過期 ???? * @return ???? */? ??? private static boolean deleteExp(String key, Date expire) {? ??????? boolean flag = false;? ??????? try {? ??????????? flag = cachedClient.delete(key, expire);? ??????? } catch (Exception e) {? ??????????? logger.error("Memcached delete方法報錯,key值:" + key + "\r\n"); ??????? }? ??????? return flag;? ??? }? ? ??? /** ???? * 清理緩存中的所有鍵/值對 ???? *? ???? * @return ???? */? ??? public static boolean flashAll() {? ??????? boolean flag = false;? ??????? try {? ??????????? flag = cachedClient.flushAll();? ??????? } catch (Exception e) {? ??????????? logger.error("Memcached flashAll方法報錯\r\n");? ??????? }? ??????? return flag;? ??? }? ? ??? /*@Test ??? public void testMemcachedSpring() {? ??????? MemcachedUtils.set("aa", "bb", new Date(1000 * 60));? ??????? Object obj = MemcachedUtils.get("aa");? ??????? System.out.println("***************************");? ??????? System.out.println(obj.toString());? ??? }*/ ???????? } |
5 SpringMVC中調用的方式:
| @RequestMapping(value = "/toIndex") ???????? public String toIndex(Model model) { ???????????????? //方法一,這種不建議使用 ???????????????? //MemCachedClient memCachedClient = new MemCachedClient("memcache"); ???????? ???? //memCachedClient.set("name", "simple"); ???????? ???? //System.out.println(memCachedClient.get("name")); ???????? ??? ??????????????? //方法二,建議這種 ???????? ??? MemcachedUtils.set("name", "simple"); ???????? ??? String name = (String)MemcachedUtils.get("name"); ???????? ??? System.out.println(name); ?????????????????? ???????? ??? return "/admin/index"; ???????? } |
?
總結
以上是生活随笔為你收集整理的Memcached的配置,SSH项目中的整合(com.whalin),Memcached工具类,Memcached的代码调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鹅蛋怎样做好吃又有营养?
- 下一篇: 家常炖牛腩的做法?