下面来一个高度封装的memcached工具类;如下:
package com.hoodong.framework.cache;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import com.hoodong.framework.util.FrameProperties;
public class MemCached {
private static Map cachePool = new ConcurrentHashMap();
/**
* 取得指定memcached.
* @param cacheName
* @return
*/
public static MemCachedClient getCache(String cacheName) {
if (cacheName == null && "".equals(cacheName.trim())) {
return null;
}
if (cachePool.get(cacheName) == null
|| !(cachePool.containsKey(cacheName))) {
String fileName=cacheName+".xml";
MemCachedClient cacheClient = initMemecached(fileName);
if (cacheClient == null) {
return null;
} else {
cachePool.put(cacheName, cacheClient);
return cacheClient;
}
}
return (MemCachedClient) cachePool.get(cacheName);
}
// 设置与缓存服务器的连接池
private static MemCachedClient initMemecached(String filename) {
String poolName = FrameProperties.getProperties(filename, "poolName");
// 得到一个实例对象SockIOPool
SockIOPool pool = SockIOPool.getInstance(poolName);
//先判断pool是否已经被初始化
if(!pool.isInitialized()){
String serverList = FrameProperties.getProperties(filename, "serverList");
String weightList = FrameProperties.getProperties(filename, "weightList");
String minConn = FrameProperties.getProperties(filename, "minConn");
String initConn = FrameProperties.getProperties(filename, "initConn");
String maxConn = FrameProperties.getProperties(filename, "maxConn");
String maxIdle = FrameProperties.getProperties(filename, "maxIdle");
String maintSleep = FrameProperties.getProperties(filename, "maintSleep");
String socketTO = FrameProperties.getProperties(filename, "socketTO");
String socketConnectTO = FrameProperties.getProperties(filename, "socketConnectTO");
String aliveCheck = FrameProperties.getProperties(filename, "aliveCheck");
String nagle = FrameProperties.getProperties(filename, "nagle");
String failover = FrameProperties.getProperties(filename, "failover");
String failback = FrameProperties.getProperties(filename, "failback");
String[] serversStr = serverList.split(",");
String[] servers = new String[serversStr.length];
for (int i = 0; i < servers.length; i++) {
servers[i] = serversStr[i];
}
String[] weightsStr = weightList.split(",");
Integer[] weights = new Integer[weightsStr.length];
for (int i = 0; i < weights.length; i++) {
weights[i] = new Integer(weightsStr[i]);
}
// set the servers and the weights
// 设置Memcached Server
pool.setServers(servers);
pool.setWeights(weights);
// set some basic pool settings
// 5 initial, 5 min, and 250 max conns
// and set the max idle time for a conn
// to 6 hours
pool.setInitConn(Integer.parseInt(initConn));
pool.setMinConn(Integer.parseInt(minConn));
pool.setMaxConn(Integer.parseInt(maxConn));
pool.setMaxIdle(Long.parseLong(maxIdle));
// set the sleep for the maint thread
// it will wake up every x seconds and
// maintain the pool size
pool.setMaintSleep(Long.parseLong(maintSleep));
// Tcp的规则就是在发送一个包之前,本地机器会等待远程主机
// 对上一次发送的包的确认信息到来;这个方法就可以关闭套接字的缓存,
// 以至这个包准备好了就发;
pool.setNagle(Boolean.parseBoolean(nagle));
// 连接建立后对超时的控制
pool.setSocketTO(Integer.parseInt(socketTO));
// 连接建立时对超时的控制
pool.setSocketConnectTO(Integer.parseInt(socketConnectTO));
pool.setAliveCheck(Boolean.parseBoolean(aliveCheck));
pool.setFailover(Boolean.parseBoolean(failover));
pool.setFailback(Boolean.parseBoolean(failback));
pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);
// initialize the connection pool
// 初始化一些值并与MemcachedServer段建立连接
// 初始化连接池
pool.initialize();
}
// 压缩设置,超过指定大小(单位为K)的数据都会被压缩
MemCachedClient mcc = new MemCachedClient(poolName);
String primitiveAsString = FrameProperties.getProperties(filename, "primitiveAsString");
String compressEnable = FrameProperties.getProperties(filename, "compressEnable");
mcc.setCompressEnable(Boolean.parseBoolean(compressEnable));
//mcc.setCompressThreshold(64 * 1024);//默认15k
mcc.setPrimitiveAsString(Boolean.parseBoolean(primitiveAsString));
mcc.setSanitizeKeys(false);
return mcc;
}
/**
* 销毁缓存
*/
protected void finalize() throws Throwable {
cachePool.clear();
cachePool=null;
super.finalize();
}
public static void main(String a[])
{
MemCachedClient m= MemCached.getCache("mclient0");
m.set("a", 1);
m.set("b", 2);
m.set("c", 3);
System.out.println(m.get("a"));
System.out.println(m.get("b"));
System.out.println(m.get("c"));
}
}
转载请指明出处:http://blog.csdn.net/yangkai_hudong
java中一个memcached案例,布布扣,bubuko.com
原文地址:http://blog.csdn.net/yangkai_hudong/article/details/25039599