Java Client calls memcached

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.