Package Memcached;import Java.io.ioexception;import Java.net.inetsocketaddress;import Net.spy.memcached.memcachedclient;public class Client {private final int expiredseconds = 100;private final Inetsocketaddress Server = new Inetsocketaddress ("127.0.0.1", 10101);p rivate memcachedclient memcachedclient;public void Init () {try {memcachedclient = new memcachedclient (server);} catch (IOException e) {e.printstacktrace ();}} public void put (String key, Object obj) {memcachedclient.set (key, expiredseconds, obj);} public void Delete (String key) {memcachedclient.delete (key);} Public Object get (String key) {return memcachedclient.get (key);} public static void Main (String args[]) {Client c = new Client (), C.init (); Long begin = System.currenttimemillis (); for (int i =0;i<100;i++) {c.put (i+ "", I); System.out.println (C.get (i+ "));} System.out.println ((System.currenttimemillis ()-begin) + "MS");}}
The first is to set the expiration time, in seconds, note that when put, the value as a parameter of set, but if the value is greater than 30 days of the number of seconds, it will be the Unix time to set the expiration time instead of offset.
The second line is a local memcached server address and port, call Memcacedclient instantiate a Memcachedclient object, the following is the custom Put,get,delete common method can be
The direct results are as follows:
2014-08-22 09:35:01.891 INFO net.spy.memcached.MemcachedConnection:Added {QA sa=/127.0.0.1:10101, #Rops =0, #Wops = 0, # Iq=0, Toprop=null, Topwop=null, towrite=0, interested=0} to connect queue2014-08-22 09:35:01.902 WARN Net.spy.memcached.MemcachedConnection:Could not redistribute to another node, retrying primary node for 0.2014-08-22 09: 35:01.904 WARN net.spy.memcached.MemcachedConnection:Could not redistribute to another node, retrying primary node for 0 .012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 656667686970717273747576777879808182838485868788899091929394959697989992ms