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.     }  

猜你喜欢

转载自hougechuanqi.iteye.com/blog/1930251
今日推荐