java操作memcached入門教程demo代碼

來源:互聯網
上載者:User

標籤: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代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.