1. memcached Client for Java clients api:memcached for Java
Introducing JAR Package: Java-memcached-2.6.2.jar
Packagecom.pcitc.memcached;Importcom.danga.memcached.*; Public classtestmemcached { Public Static voidMain (string[] args) {/*Initialize the Sockiopool, manage the memcached connection pool*/string[] Servers= {"192.168.1.111: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 (); /*establishing Memcachedclient Instances*/memcachedclient memcachedclient=Newmemcachedclient (); for(inti = 0; I < 10; i++) { /*adding an object to the memcached cache*/ BooleanSuccess = Memcachedclient.set ("" + I, "hello!"); /*To take an object by key value from the memcached cache*/String Result= (String) memcachedclient.get ("" +i); System.out.println (String.Format ("Set (%d):%s", I, success)); System.out.println (String.Format ("Get (%d):%s", I, result)); } }}
2. spymemcached clients api:spymemcached Client
Introducing JAR Package:Spymemcached-2.10.3.jar
Packagecom.pcitc.memcached;Importjava.net.InetSocketAddress;Importjava.util.concurrent.Future;Importnet.spy.memcached.MemcachedClient; Public classmclient { Public Static voidMain (string[] args) {setValue (); GetValue (); } //storing objects in cache with spymemcached Public Static voidSetValue () {Try { /*establish an Memcachedclient instance and specify the IP address and port number of the memcached service*/memcachedclient MC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211)); Future<Boolean> B =NULL; /*set the key value, the expiration time (in seconds), and the object to be cached to memcached*/b= Mc.set ("Neead", "Someobject"); if(B.get (). Booleanvalue () = =true) {Mc.shutdown (); } } Catch(Exception ex) {ex.printstacktrace (); } } //using spymemcached to get objects from the cache Public Static voidGetValue () {Try { /*establish an Memcachedclient instance and specify the IP address and port number of the memcached service*/memcachedclient MC=NewMemcachedclient (Newinetsocketaddress ("192.168.1.111", 11211)); /*finds cache from memcached by key value, returns null if not present*/Object b= Mc.get ("Neead"); Mc.shutdown (); } Catch(Exception ex) {ex.printstacktrace (); } }}
3. Two different API comparisons
memcached Client for Java: The memcached Java Client API, which has been introduced earlier, is widely used and runs more stably.
Spymemcached:a simple, asynchronous, single-threaded memcached client written in Java. Support asynchronous, single-threaded memcached client, using the java1.5 version of concurrent and NIO, access speed will be higher than the former, but the stability is not good, test often reported timeout and other related anomalies.
memcached Client for Java is recommended because memcached Client for Java publishes a new version, improves performance and runs stably.
Java Client calls memcached