import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import java.lang.Boolean;
import java.util.Date;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class Memcache {
static SockIOPool pool = null;
static MemCachedClient mc;
private static Logger logger = LogManager.getLogger(Memcache.class);
static {
try {
String serverlists = MemcacheConfig.getValues("servers");
String[] serverlist = serverlists.split(";");
pool = SockIOPool.getInstance();
pool.setServers(serverlist);
pool.setInitConn(Integer.parseInt((MemcacheConfig
.getValues("initconn"))));
pool.setMinConn(Integer.parseInt(MemcacheConfig
.getValues("minconn")));
pool.setMaxConn(Integer.parseInt(MemcacheConfig
.getValues("maxconn")));
pool.setMaintSleep(Integer.parseInt(MemcacheConfig
.getValues("maintsleep")));
pool.setNagle(Boolean.getBoolean(MemcacheConfig
.getValues("nagle")));
boolean flag = pool.isInitialized();
if(!flag)
{
pool.initialize();
}
mc = new MemCachedClient();
mc.setCompressEnable(Boolean.getBoolean(MemcacheConfig
.getValues("compressEnable")));
mc.setCompressThreshold(0);
} catch (Exception e) {
logger.error(e);
}
}
/**
* 设置cache
*
* @param key
* @param value
* @param mill
*/
public static void setCache(String key, Object value, long mill) {
if (mill == 0) {
mc.set(key, value);
} else {
mc.set(key, value, new Date(mill));
}
}
/**
* 获取cache内容
*
* @param key
* @return
*/
public static Object getCache(String key) {
Object obj = mc.get(key);
return obj;
}
/**
* 删除cache
*
* @param key
*/
public static void delCache(String key) {
mc.delete(key);
}
public static void main(String args[]) {
Memcache.setCache("user2", "1111", 0);
System.out.println(Memcache.getCache("user2"));
}
}