下面的Java代码,演示了三种常用的Memcached常用驱动的连接方式,所有驱动现在都托管在GitHub上面,大家可以自行查找。
package com.neohope.memcached.test; import java.util.concurrent.TimeoutException; import java.io.IOException; import net.rubyeye.xmemcached.utils.AddrUtil; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.MemcachedClientBuilder; import net.rubyeye.xmemcached.XMemcachedClientBuilder; import net.rubyeye.xmemcached.exception.MemcachedException; import java.net.InetSocketAddress; import net.spy.memcached.*; import com.whalin.MemCached.SockIOPool; import com.whalin.MemCached.MemCachedClient; public class Main { //MemcachedClietForJava public static void TestMemcachedClietForJava() { String[] servers = {"127.0.0.1:11211"}; SockIOPool pool = SockIOPool.getInstance(); pool.setServers(servers); pool.setFailover(true); pool.setInitConn(10); pool.setMinConn(5); pool.setMaxConn(250); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(3000); pool.setAliveCheck(true); pool.initialize(); MemCachedClient memcachedClient = new MemCachedClient(); for(int i=0;i<100;i++) { memcachedClient.set("key"+i, "Hello,MemcachedClietForJava"); } for(int i=0;i<100;i++) { String value = memcachedClient.get("key" + i).toString(); System.out.println("key"+i+" = " + value); memcachedClient.delete("key" + i); } } //SpyMemcache public static void TestSpyMemcache() { net.spy.memcached.MemcachedClient memcachedClient = null; try { memcachedClient = new net.spy.memcached.MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); for(int i=0;i<100;i++) { memcachedClient.set("key"+i, 0, "Hello,SpyMemcache"); } for(int i=0;i<100;i++) { String value = memcachedClient.get("key" + i).toString(); System.out.println("key"+i+" = " + value); memcachedClient.delete("key" + i); } } catch (IOException ex) { System.err.println("New MemcachedClient fail"); ex.printStackTrace(); } finally { memcachedClient.shutdown(); } } //XMemcache public static void TestXMemcache() { MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("127.0.0.1:11211")); MemcachedClient memcachedClient = null; try { memcachedClient = builder.build(); for(int i=0;i<100;i++) { memcachedClient.set("key" + i, 0, "Hello,XMemcache"); } for(int i=0;i<100;i++) { String value = memcachedClient.get("key" + i); System.out.println("key" + i + " = " + value); memcachedClient.delete("key" + i); } } catch (MemcachedException e) { System.err.println("MemcachedClient operation fail"); e.printStackTrace(); } catch (TimeoutException e) { System.err.println("MemcachedClient operation timeout"); e.printStackTrace(); } catch (InterruptedException e) { System.err.println("MemcachedClient operation Interrupted"); e.printStackTrace(); } catch (IOException e) { System.err.println("Build MemcachedClient fail"); e.printStackTrace(); } finally { if(memcachedClient!=null) { try { memcachedClient.shutdown(); } catch (IOException ex) { } } } } public static void main(String[] args) { TestXMemcache(); TestSpyMemcache(); TestMemcachedClietForJava(); } }