memcachedDemo

package com.bennytian.memcached;
002  
003 import java.io.Serializable;
004 import java.util.Date;
005  
006 import com.danga.MemCached.MemCachedClient;
007 import com.danga.MemCached.SockIOPool;
008  
009 @SuppressWarnings("serial")
010 public class MemCached implements Serializable {
011  
012     private static MemCachedClient mcc = new MemCachedClient(); 
013     private static MemCached memCached = new MemCached();
014      
015     //服务区群
016     private static final String [] services = {"127.0.0.1:11211"};
017     //服务器群各自对应的权重
018     private static final Integer[] weights = {1};
019     //初始化连接数
020     private static final Integer init_connection = 5;
021     //最小连接数
022     private static final Integer min_connection = 5;
023     //最大连接数
024     private static final Integer max_connection = 250;
025     //最大处理时间  60*60*6
026     private static final Long max_idle = 21600L ;
027     //主程序睡眠时间
028     private static final Long maint_sleep = 10L;
029      
030     static{
031         // 获取socke连接池的实例对象 
032         SockIOPool pool = SockIOPool.getInstance();
033         pool.setServers(services);
034         pool.setWeights(weights);
035         pool.setInitConn(init_connection); 
036         pool.setMinConn(min_connection); 
037         pool.setMaxConn(max_connection); 
038         pool.setMaxIdle(max_idle); 
039         pool.setMaintSleep(maint_sleep);
040          
041         // 设置TCP的参数,连接超时等 
042         pool.setNagle(false);
043         pool.setSocketTO(3000);
044         pool.setSocketConnectTO(0);
045   
046         // 初始化连接池 
047         pool.initialize();
048          
049         //超过指定大小就压缩
050         mcc.setCompressEnable(true);
051         mcc.setCompressThreshold(65536L);
052     }
053      
054     private MemCached(){}
055      
056     public static MemCached getInstance(){
057         return memCached;
058     }
059      
060     public boolean add(String key,Object value){
061         return mcc.add(key, value);
062     }
063      
064     public boolean add(String key,Object value,Date expiry){
065         return mcc.add(key, value, expiry);
066     }
067      
068     public boolean replace(String key, Object value){ 
069         return mcc.replace(key, value); 
070     
071       
072     public boolean replace(String key, Object value, Date expiry){ 
073         return mcc.replace(key, value, expiry); 
074     
075       
076     public Object get(String key){ 
077         return mcc.get(key); 
078     }
079      
080     public static void main(String[] args)  { 
081         MemCached cache = MemCached.getInstance(); 
082         Date d = new Date();
083         cache.add("11", cache); 
084         cache.add("12"new String("1234")); 
085         /*for (int i = 0; i < 1000; i++) {
086             cache.add(i+"", cache); 
087         }*/
088         System.out.println(cache.get("11"));
089         System.out.println(cache.get("12"));
090         System.out.println(System.currentTimeMillis()-d.getTime());
091         //System.out.print("get value : " + cache.get("hello")); 
092         //System.out.println(cache.get("100"));
093     
094     /*
095         安装命令
096     c:>memcached.exe -d install 
097         启动兼分配内存
098     c:>memcached.exe -l 127.0.0.1 -m 32 -d start 
099      
100     */
101 }

 

转载于:https://my.oschina.net/u/580135/blog/612235

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值