Memcached学习(三)
通过Java客户端实现与Memcached的交互,Java客户端的实现了使用了开源的Memcached-Java-Client,开源地址在GitHub上。
如下是通过该开源库实现的Memcached交互demo。
1 import com.schooner.MemCached.SchoonerSockIOPool; 2 import com.whalin.MemCached.MemCachedClient; 3 import com.whalin.MemCached.SockIOPool; 4 5 public class TestMem { 6 7 private MemCachedClient mClient; 8 9 public static void main(String[] arg) { 10 TestMem testMem = new TestMem(); 11 testMem.test(); 12 } 13 14 public TestMem() { 15 String[] servers ={"127.0.0.1:11211", "127.0.0.1:11212", "127.0.0.1:11213", "127.0.0.1:11214"}; 16 SockIOPool pool = SockIOPool.getInstance(); 17 pool.setServers(servers); 18 pool.setFailover(true); 19 pool.setInitConn(5); 20 pool.setMinConn(2); 21 pool.setMaxConn(10); 22 pool.setMaintSleep(30); 23 pool.setNagle(false); 24 pool.setSocketTO(3000); 25 pool.setAliveCheck(true); 26 pool.setHashingAlg(SchoonerSockIOPool.CONSISTENT_HASH); 27 pool.initialize(); 28 29 mClient = new MemCachedClient(true); 30 } 31 32 public void test() { 33 boolean isSucc = false; 34 isSucc = mClient.set("mKey", "mValue"); 35 log("isSucc:" + isSucc); 36 37 log("obj:" + mClient.get("mKey")); 38 } 39 40 public static void log(Object obj) { 41 System.out.println(obj); 42 } 43 }