package com.ibs.auth.controller;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.whalin.memcached.MemCachedClient;
import com.whalin.memcached.SockIOPool;
public class MemCachedManager {
// 创建全局的唯一实例
protected static MemCachedClient mcc = new MemCachedClient();
protected static MemCachedManager memCachedManager = new MemCachedManager();
// 设置与缓存服务器的连接池
static {
// 服务器列表和其权重
String[] servers = { "127.0.0.1:11211" };
Integer[] weights = { 3 };
// 获取socke连接池的实例对象
SockIOPool pool = SockIOPool.getInstance();
// 设置服务器信息
pool.setServers(servers);
pool.setWeights(weights);
// 设置初始连接数、最小和最大连接数以及最大处理时间
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000 * 60 * 60 * 6);
//设置主线程睡眠时间,每3秒苏醒一次,维持连接池大小
//maintSleep 千万不要设置成30,访问量一大就出问题,单位是毫秒,推荐30000毫秒。
pool.setMaintSleep(30);
// 设置TCP的参数,连接超时等
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
// 初始化连接池
pool.initialize();
// 压缩设置,超过指定大小(单位为K)的数据都会被压缩
//mcc.setCompressEnable(true);
//mcc.setCompressThreshold(64 * 1024);
}
/**
* 保护型构造方法,不允许实例化!
*
*/
protected MemCachedManager() {
}
/**
* 获取唯一实例.
*
* @return
*/
public static MemCachedManager getInstance() {
return memCachedManager;
}
/**
* 添加一个指定的值到缓存中.
*
* @param key
* @param value
* @return
*/
public boolean add(String key, Object value) {
return mcc.add(key, value);
}
public boolean add(String key, Object value, Date expiry) {
return mcc.add(key, value, expiry);
}
public boolean replace(String key, Object value) {
return mcc.replace(key, value);
}
public boolean replace(String key, Object value, Date expiry) {
return mcc.replace(key, value, expiry);
}
public boolean set(String key,Object value){
return mcc.set(key, value);
}
/**
* 根据指定的关键字获取对象.
*
* @param key
* @return
*/
public Object get(String key) {
return mcc.get(key);
}
/**
* 删除key
*/
public boolean delete(String key){
return mcc.delete(key);
}
public static Map<String,Object> getKeysForMap() throws UnsupportedEncodingException{
Map<String,Object> keylist=new HashMap<String,Object>();
//遍历statsItems 获取items:2:number=14
Map<String,Map<String,String>> statsItems=mcc.statsItems();
Map<String,String> statsItems_sub=null;
String statsItems_sub_key=null;
int items_number=0;
String server=null;
//根据items:2:number=14,调用statsCacheDump,获取每个item中的key
Map<String,Map<String,String>> statsCacheDump=null;
Map<String,String> statsCacheDump_sub=null;
String statsCacheDumpsub_key=null;
String statsCacheDumpsub_key_value=null;
for (Iterator iterator=statsItems.keySet().iterator();iterator.hasNext();) {
server=(String) iterator.next();
statsItems_sub=statsItems.get(server);
//System.out.println(server+"==="+statsItems_sub);
for (Iterator iterator_item=statsItems_sub.keySet().iterator();iterator_item.hasNext();) {
statsItems_sub_key=(String) iterator_item.next();
//System.out.println(statsItems_sub_key+":=:"+bb);
//items:2:number=14
if (statsItems_sub_key.toUpperCase().startsWith("items:".toUpperCase()) && statsItems_sub_key.toUpperCase().endsWith(":number".toUpperCase())){
items_number=Integer.parseInt(statsItems_sub.get(statsItems_sub_key).trim());
//System.out.println(statsItems_sub_key+":=:"+items_number);
statsCacheDump=mcc.statsCacheDump(new String[]{server},Integer.parseInt(statsItems_sub_key.split(":")[1].trim()), items_number);
for (Iterator statsCacheDump_iterator=statsCacheDump.keySet().iterator();statsCacheDump_iterator.hasNext();) {
statsCacheDump_sub=statsCacheDump.get(statsCacheDump_iterator.next());
//System.out.println(statsCacheDump_sub);
for (Iterator iterator_keys=statsCacheDump_sub.keySet().iterator();iterator_keys.hasNext();) {
statsCacheDumpsub_key=(String) iterator_keys.next();
statsCacheDumpsub_key_value=statsCacheDump_sub.get(statsCacheDumpsub_key);
System.out.println(statsCacheDumpsub_key);//key是中文被编码了,是客户端在set之前编码的,服务端中文key存的是密文
System.out.println(statsCacheDumpsub_key_value);
keylist.put(statsCacheDumpsub_key,statsCacheDumpsub_key_value);
} }
}
}
}
return keylist;
}
public static void main(String[] args) {
MemCachedManager cache = MemCachedManager.getInstance();
try {
Map<String,Object> map=getKeysForMap();
for(int i=0;i<map.size();i++){
if(map.containsKey("1")){
System.out.println("it‘s OK");
}
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cache.add("hello", 234);
System.out.print("get value : " + cache.get("hello"));
}
}