java:spymemcached客户端下载:
https://code.google.com/archive/p/spymemcached/?spm=a2c4g.11186623.2.5.IU7oGl
java示例代码1
(免密访问,需要把spymemcached的jar包添加到工程目录下才能编译运行):
packageSpymemcached;importjava.net.InetSocketAddress;importnet.spy.memcached.MemcachedClient;importnet.spy.memcached.internal.OperationFuture;publicclassSpymemcached{publicstaticvoidmain(String[]args){try{MemcachedClientmcc=newMemcachedClient(newInetSocketAddress("ip",port));System.out.println("Connectiontoserversucessfully.");OperationFuturefo=mcc.set("key",0,"value");System.out.println("setstatus:"+fo.get());System.out.println("keyvalue-"+mcc.get("key"));mcc.shutdown();}catch(Exceptionex){System.out.println(ex.getMessage());}}}
java示例代码2
(密码访问,需要把spymemcached的jar包添加到工程目录下才能编译运行):
packageSpymemcached;importjava.net.InetSocketAddress;importnet.spy.memcached.AddrUtil;importnet.spy.memcached.ConnectionFactoryBuilder;importnet.spy.memcached.ConnectionFactoryBuilder.Protocol;importnet.spy.memcached.MemcachedClient;importnet.spy.memcached.auth.AuthDescriptor;importnet.spy.memcached.auth.PlainCallbackHandler;importnet.spy.memcached.internal.OperationFuture;publicclassSpymemcached{publicstaticvoidmain(String[]args){try{AuthDescriptorad=newAuthDescriptor(newString[]{"PLAIN"},newPlainCallbackHandler("test","test"));MemcachedClientmcc=newMemcachedClient(newConnectionFactoryBuilder().setProtocol(Protocol.BINARY).setAuthDescriptor(ad).build(),AddrUtil.getAddresses("ip"+":"+port));System.out.println("Connectiontoserversucessful.");OperationFuturefo=mcc.set("key",0,"value");System.out.println("setstatus:"+fo.get());System.out.println("keyvalue-"+mcc.get("key"));mcc.shutdown();}catch(Exceptionex){System.out.println(ex.getMessage());}}}
发表评论