Memcached客户端utils类

0.个人标签

我的CSDN博客地址:  http://blog.csdn.net/caicongyang 

1.依赖的jar包

commons-pool-1.5.6.jar

slf4j-api-1.6.1.jar

java_memcached-release_2.6.6.jar

slf4j-simple-1.6.1.jar

2.DangaMemCacheClient.java

package com.ccy.comm.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

/**
 * 

* Title: DangaMemCacheClient.java * Package *

*

* Description: MemCacheClient Utils类 *

* @author Tom.Cai * @created 2015-7-16 下午3:49:24 * @version V1.0 * */ public class DangaMemCacheClient { private static DangaMemCacheClient instance = null; private static ReentrantLock lock = new ReentrantLock(); private MemCachedClient mc; private static int expTime = 3600 * 24 * 30; private static String address="192.168.253.132"; private static String port="12000"; public static DangaMemCacheClient getInstance() { if (instance == null) { lock.lock(); try { if (instance == null) { instance = new DangaMemCacheClient(); } } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } return instance; } private DangaMemCacheClient() { try { mc = new MemCachedClient(); String[] servers = { address+":"+port }; Integer[] weights = { 10 }; SockIOPool pool = SockIOPool.getInstance(); pool.setServers(servers); pool.setWeights(weights); pool.setInitConn(5); pool.setMinConn(5); pool.setMaxConn(250); pool.setMaxIdle(expTime); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(30); pool.setSocketConnectTO(0); pool.initialize(); } catch (Exception e) { e.printStackTrace(); } } public Object getObject(String key) { return mc.get(key); } public boolean setObject(String key, Object value) { return mc.set(key, value); } public boolean setObject(String key, Object value, long time) { return mc.set(key, value, new Date(time)); } public boolean removeObject(String key) { return mc.delete(key); } /** * 循环获取所有Memcache中的所有key * @return */ public List getKeys() { List keyList = new ArrayList(); Map slabs = mc.statsItems(); Iterator itemsItr = slabs.keySet().iterator(); while (itemsItr.hasNext()) { String serverInfo1 = itemsItr.next().toString(); // 取得這個server的各種 status [itemname:number:field=value] Map itemNames = (Map) slabs.get(serverInfo1); Iterator itemNameItr = itemNames.keySet().iterator(); // 以status key值去迴圈 while (itemNameItr.hasNext()) { String itemName = itemNameItr.next().toString(); // 拆解status 欄位 // itemAtt[0] = itemname // itemAtt[1] = CacheDump的參數 // itemAtt[2] = field:number or age String[] itemAtt = itemName.split(":"); if (itemAtt[2].startsWith("number")) { Map chcheDump = mc.statsCacheDump(Integer.parseInt(itemAtt[1]), 0); Iterator itr = chcheDump.keySet().iterator(); int i = 0; while (itr.hasNext()) { String serverInfo2 = itr.next().toString(); Map items = (Map) chcheDump.get(serverInfo2); Iterator keyItr = items.keySet().iterator(); while (keyItr.hasNext()) { String key = keyItr.next().toString(); String memKey = key; i++; try { key = URLDecoder.decode(key, "UTF-8"); String value = ((String) items.get(memKey)); value = value.substring(value.indexOf(";") + 2, value.indexOf(" s")); Date date = new Date(); date.setTime(Long.valueOf(value + "000")); keyList.add(key); } catch (Exception ex) { } } } } } } return keyList; } public static void main(String[] args) throws UnsupportedEncodingException { DangaMemCacheClient dmcc = DangaMemCacheClient.getInstance(); dmcc.setObject("ccy0", "test0"); dmcc.setObject("ccy1", "test1"); System.out.println((String)dmcc.getObject("ccy0")); System.out.println((String)dmcc.getObject("ccy1")); List list = dmcc.getKeys(); for(String key : list){ System.out.println(key); } } }


3.说明

本人将其写成一个工具来适用于只缓存不存数据库的情况,当然可以写一个service在供spring来调用!



我的CSDN博客地址:  http://blog.csdn.net/caicongyang 



你可能感兴趣的:(cache)