- // create a static client as most installs only need
- // a single instance
- protected static MemCachedClient mcc = new MemCachedClient();
- // set up connection pool once at class load
- static {
- // server list and weights
- String[] servers = { "localhost:11211", "localhost:11212", "localhost:11213" };
- Integer[] weights = { 3, 3, 2 };
- // grab an instance of our connection pool
- SockIOPool pool = SockIOPool.getInstance();
- // set the servers and the weights
- pool.setServers(servers);
- pool.setWeights(weights);
- pool.setHashingAlg(SockIOPool.CONSISTENT_HASH);
- // set some basic pool settings
- // 5 initial, 5 min, and 250 max conns
- // and set the max idle time for a conn
- // to 6 hours
- pool.setInitConn(5);
- pool.setMinConn(5);
- pool.setMaxConn(250);
- pool.setMaxIdle(1000 * 60 * 60 * 6);
- // set the sleep for the maint thread
- // it will wake up every x seconds and
- // maintain the pool size
- pool.setMaintSleep(30);
- // set some TCP settings
- // disable nagle
- // set the read timeout to 3 secs
- // and don't set a connect timeout
- pool.setNagle(false);
- pool.setSocketTO(3000);
- pool.setSocketConnectTO(0);
- // initialize the connection pool
- pool.initialize();
- }
- public static void main(String[] args) {
- System.out.println("SET: " + mcc.set("key1", "value1"));
- System.out.println("SET: " + mcc.set("key2", "value2"));
- System.out.println("SET: " + mcc.set("key3", "value3"));
- System.out.println("GET: " + mcc.get("key1"));
- MemcachedItem item = mcc.gets("key1");
- System.out.println("GETS: value=" + item.getValue() + ",CasUnique:"+item.getCasUnique());
- System.out.println("SET: " + mcc.set("key1", "value1_1"));
- System.out.println("CAS: " + mcc.cas("key1", "value1_2", item.getCasUnique())); //必须FALSE
- System.out.println("getMulti:" + mcc.getMulti(new String[]{"key1","key2","key3"}));
- }
memcached client
猜你喜欢
转载自hougechuanqi.iteye.com/blog/1930251
今日推荐
周排行