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. Unit is seconds, note here at the time of put. This value is a parameter of set, but assumes that the value is greater than the number of seconds in 30 days. It will be considered Unix time to set the expiration time instead of offset.
The second line is a local memcached server address and port, calling Memcacedclient to instantiate a Memcachedclient object, and the following defines the put. Get,delete can be used in a regular usage.
Direct results such as the following:
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
NET Spy memcached using demo