首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

java联接memcache

2012-07-23 
java连接memcache2009-03-10 09:29:10|??分类: java程序设计 |??标签: |字号大中小?订阅 ?import com.dang

java连接memcache
2009-03-10 09:29:10|??分类: java程序设计 |??标签: |字号大中小?订阅

?

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"));
????
?}
}

热点排行