Memcached Java Client

测试

public void test() {
		for(int i=0;i<100000;i++){
			UserBean u = new UserBean("张三", "xsd"+i);
			MemcachedUtil.put("u"+i, u);
		}
		
		for(int i=99999;i<=100000;i++){
			Object o = MemcachedUtil.get("u"+i);
			if(o !=null){
				UserBean u = (UserBean) o;
				System.out.println(u.getUsername()+":"+u.getPassword());
			}
		}
		
	}

客户端

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

public class MemcachedUtil {  
	  
    /** 
     * memcached客户端单例 
     */  
    private static MemCachedClient cachedClient = new MemCachedClient();  
      
    /** 
     * 初始化连接池 
     */  
    static {  
        //获取连接池的实例  
        SockIOPool pool = SockIOPool.getInstance();  
          
        //服务器列表及其权重  
        String[] servers = {"127.0.0.1:11211","192.168.80.91:11211"};  
        Integer[] weights = {1,1};  
          
        //设置服务器信息  
        pool.setServers(servers);  
        pool.setWeights(weights);  
          
        //设置初始连接数、最小连接数、最大连接数、最大处理时间  
        pool.setInitConn(10);  
        pool.setMinConn(10);  
        pool.setMaxConn(1000);  
        pool.setMaxIdle(1000*60*60);  
          
        //设置连接池守护线程的睡眠时间  
        pool.setMaintSleep(60);  
          
        //设置TCP参数,连接超时  
        pool.setNagle(false);  
        pool.setSocketTO(60);  
        pool.setSocketConnectTO(0);  
          
        //初始化并启动连接池  
        pool.initialize();  
          
        //压缩设置,超过指定大小的都压缩  
//      cachedClient.setCompressEnable(true);  
//      cachedClient.setCompressThreshold(1024*1024);  
    }  
      
    private MemcachedUtil(){  
    }  
      
    public static boolean add(String key, Object value) {  
        return cachedClient.add(key, value);  
    }  
      
    public static boolean add(String key, Object value, Integer expire) {  
        return cachedClient.add(key, value, expire);  
    }  
      
    public static boolean put(String key, Object value) {  
        return cachedClient.set(key, value);  
    }  
      
    public static boolean put(String key, Object value, Integer expire) {  
        return cachedClient.set(key, value, expire);  
    }  
      
    public static boolean replace(String key, Object value) {  
        return cachedClient.replace(key, value);  
    }  
      
    public static boolean replace(String key, Object value, Integer expire) {  
        return cachedClient.replace(key, value, expire);  
    }  
      
    public static Object get(String key) {  
        return cachedClient.get(key);  
    }  
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值