標籤:memcached cache
原文:java操作memcached入門教程demo代碼
原始碼:http://www.zuidaima.com/share/1550463754996736.htm
參考地址:
http://www.open-open.com/lib/view/open1357831114183.html
http://tech.idv2.com/2008/07/10/memcached-001/
感謝 京-java牛-ID號1 感謝 錫-SuperPrivate-195
在網上搜尋了部分代碼,一個很簡單的Demo,將查詢的結果集存入緩衝。我也不是太懂,大家可以搜尋下資料,不過加入MemCached後項目訪問速度的確增加了
package com.dream.core.util;import java.util.Date;import com.danga.MemCached.MemCachedClient;import com.danga.MemCached.SockIOPool;/** * @describe:MemoryUtil.java * * @date 2013-9-30 * @author yuanlin.su www.zuidaima.com * 參考地址:http://www.open-open.com/lib/view/open1357831114183.html * http://tech.idv2.com/2008/07/10/memcached-001/ * 感謝 京-java牛-ID號1 * 感謝 錫-SuperPrivate-195 * * 服務端和jar包免積分:http://download.csdn.net/detail/consuy/6341989 */public class MemoryUtil {private static MemCachedClient client = null;private final static String CACHE_ISOPEN_KEY = "CACHE_ISOPEN";/** * 初始化MEMCACHE工具 */public synchronized static void init(String ipAddress) {if (client == null) {String[] servers = { ipAddress };Integer[] weights = { 1 };SockIOPool pool = SockIOPool.getInstance();pool.setServers(servers);pool.setWeights(weights);pool.setNagle(false);pool.setSocketTO(10000);pool.setSocketConnectTO(3000);pool.initialize();client = new MemCachedClient();set(CACHE_ISOPEN_KEY, 1);}}/** * 設定值 * * @param key * @param value */public static void set(String key, Object value) {client.set(key, value);}/** * 設定值同時添加有效期間 * * @param key * @param value * @param minute 分鐘 */public static void set(String key, Object value, int minute) {client.set(key, value, new Date(System.currentTimeMillis() + minute * 60 * 1000l));}/** * 檢查值是否存在 * * @param key * @return */public static boolean keyExists(String key) {return client.keyExists(key);}/** * 刪除記憶體資料項目的值 * * @param key */public static void delete(String key) {client.delete(key);}/** * 清空所有的資料 */public static void clear() {client.flushAll();}/** * 從記憶體中查詢對象資訊 * * @param key * @return */@SuppressWarnings("unchecked")public static <T> T get(String key) {return (T) client.get(key);}/** * 判斷MEMCACHE是否已經開啟 * * @return */public static boolean cacheIsOpen() {if (keyExists(CACHE_ISOPEN_KEY)) {return true;} else {return false;}}//demo/*public ModelAndView publishMood(@ModelAttribute("form") UserInfo user, HttpServletRequest request) {//TODOObject<T> obj = MemoryUtil.get(key);if (obj == null) {Object<T> obj = service.find(Entity.class);MemoryUtil.set(key, obj);}reutrn null;}*/}
java操作memcached入門教程demo代碼