memcached client

  1. // create a static client as most installs only need  
  2.     // a single instance  
  3.     protected static MemCachedClient mcc = new MemCachedClient();  
  4.   
  5.     // set up connection pool once at class load  
  6.     static {  
  7.   
  8.         // server list and weights  
  9.         String[] servers = { "localhost:11211", "localhost:11212", "localhost:11213" };  
  10.   
  11.         Integer[] weights = { 3, 3, 2 };  
  12.   
  13.         // grab an instance of our connection pool  
  14.         SockIOPool pool = SockIOPool.getInstance();  
  15.   
  16.         // set the servers and the weights  
  17.         pool.setServers(servers);  
  18.         pool.setWeights(weights);  
  19.         pool.setHashingAlg(SockIOPool.CONSISTENT_HASH);  
  20.   
  21.         // set some basic pool settings  
  22.         // 5 initial, 5 min, and 250 max conns  
  23.         // and set the max idle time for a conn  
  24.         // to 6 hours  
  25.         pool.setInitConn(5);  
  26.         pool.setMinConn(5);  
  27.         pool.setMaxConn(250);  
  28.         pool.setMaxIdle(1000 * 60 * 60 * 6);  
  29.   
  30.         // set the sleep for the maint thread  
  31.         // it will wake up every x seconds and  
  32.         // maintain the pool size  
  33.         pool.setMaintSleep(30);  
  34.   
  35.         // set some TCP settings  
  36.         // disable nagle  
  37.         // set the read timeout to 3 secs  
  38.         // and don't set a connect timeout  
  39.         pool.setNagle(false);  
  40.         pool.setSocketTO(3000);  
  41.         pool.setSocketConnectTO(0);  
  42.   
  43.         // initialize the connection pool  
  44.         pool.initialize();  
  45.     }  
  46.   
  47.     public static void main(String[] args) {  
  48.         System.out.println("SET: " + mcc.set("key1", "value1"));  
  49.         System.out.println("SET: " + mcc.set("key2", "value2"));  
  50.         System.out.println("SET: " + mcc.set("key3", "value3"));  
  51.         System.out.println("GET: " + mcc.get("key1"));  
  52.         MemcachedItem item = mcc.gets("key1");  
  53.         System.out.println("GETS: value=" + item.getValue() + ",CasUnique:"+item.getCasUnique());  
  54.         System.out.println("SET: " + mcc.set("key1", "value1_1"));  
  55.         System.out.println("CAS: " + mcc.cas("key1", "value1_2", item.getCasUnique())); //必须FALSE  
  56.         System.out.println("getMulti:" + mcc.getMulti(new String[]{"key1","key2","key3"}));  
  57.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值