Several Java clients for memcached

Source: Internet
Author: User

In fact, now try to memcached the client estimate will be a bit too angry, because now the general trend is on the other side of Redis.

Memcached client currently has 3 types:

    • Memcached Client for Java (has stopped updating)
    • Spymemcached (has stopped updating)
    • Xmemcached (has been updated, and support the client Shard implementation of the cluster, the basic is now its world)
      • Performance
      • Support for the complete memcached text protocol, binary protocol.
      • JMX support enables you to adjust performance parameters, dynamically add/Remove servers, view statistics, and more through Mbean.
      • Support Client Statistics
      • Supports dynamic additions and deletions of memcached nodes.
      • Supports memcached distribution: remainder distribution and consistent hash distribution.
      • More performance tuning options.
      • Xmemcached is easier to integrate with spring.
      • Supports client-side sharding for cluster implementation.

Comparison of three APIs

1. The Memcached Java Client API, which was launched earlier, is widely used and stable in operation.

2, 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.

3, Xmemcached is also based on the Java NIO client, Java NiO compared to the traditional blocking IO model, the efficiency is high (especially in high concurrency) and resource consumption is relatively small advantages. Traditional blocking IO in order to increase efficiency, a certain number of connections need to be created to form a connection pool, and NIO requires only one connection (of course, NIO can also be pooled), which in contrast reduces the overhead of thread creation and switching, which is particularly noticeable in high concurrency. So xmemcached and spymemcached in the performance are very good, in some aspects (stored data relatively small case) xmemcached than spymemcached performance is more excellent, specifically can see this Java Memcached Clients Benchmark.

Sample code

Memcached Client for Java

Official website: https://github.com/gwhalin/Memcached-Java-Client

This has stopped with the new. Quit using!

Importjava.util.Date; Importcom.danga.memcached.*;  Public classMemcachemanagerforgwhalin {//Building a cache client    Private Staticmemcachedclient cachedclient; //single-instance mode for client management classes    Private StaticMemcachemanagerforgwhalin INSTANCE =NewMemcachemanagerforgwhalin (); PrivateMemcachemanagerforgwhalin () {cachedclient=Newmemcachedclient (); //Initialize the Sockiopool, manage the memcached connection poolSockiopool pool =sockiopool.getinstance (); //set up a list of cache servers that you can specify when you use distributed caching. (This should be set to a number of different servers)String[] Servers = {"192.168.163.10:11211", "192.168.163.11:11211"//You can also use the domain name "server3.mydomain.com:1624"        };          Pool.setservers (servers); Pool.setfailover (true); Pool.setinitconn (10);//Set initial connectionPool.setminconn (5);//Setting the minimum connectionPool.setmaxconn (250);//Setting the maximum connectionPool.setmaxidle (1000 * 60 * 60 * 3);//set maximum idle time per connection 3 hoursPool.setmaintsleep (30); Pool.setnagle (false); Pool.setsocketto (3000); Pool.setalivecheck (true);      Pool.initialize (); }        /*** Get a unique instance of the cache manager * *@return      */       Public StaticMemcachemanagerforgwhalin getinstance () {returnINSTANCE; }         Public voidAdd (String key, Object value) {Cachedclient.set (key, value); }         Public voidAdd (String key, Object value,intmilliseconds)      {Cachedclient.set (key, value, milliseconds); }         Public voidRemove (String key) {cachedclient.delete (key); }         Public voidRemove (String key,intmilliseconds) {Cachedclient.delete (key, milliseconds,NewDate ()); }         Public voidUpdate (String key, Object value,intmilliseconds)      {Cachedclient.replace (key, value, milliseconds); }         Public voidUpdate (String key, Object value) {cachedclient.replace (key, value); }         PublicObject get (String key) {returnCachedclient.get (key); }         Public Static voidMain (string[] args) {//adding an object to the memcached cacheCachedclient.add ("Keke", "This is a test String"); //To take an object by key value from the memcached cacheString result = (string) cachedclient.get ("Keke");      SYSTEM.OUT.PRINTLN (result); }        }  

Spymemcached

Official website: http://code.google.com/p/spymemcached/

This is a good choice, but there's something that can't be found on GitHub.

 Public classMemcachemanagerforspyImplementsImemcachemanager {//Cache Client    Privatememcachedclient memcachecient; //Manager Management Object, Singleton mode    Private StaticMemcachemanagerforspy INSTANCE =NewMemcachemanagerforspy (); PrivateMemcachemanagerforspy () {Try{memcachecient=NewMemcachedclient (Newinetsocketaddress ("127.0.0.1", 11211)); } Catch(IOException e) {e.printstacktrace (); }      }         Public StaticMemcachemanagerforspy getinstance () {returnINSTANCE; } @Override Public voidAdd (String key, Object value,intmilliseconds)      {Memcachecient.add (key, milliseconds, value); } @Override Public voidAdd (String key, Object value) {Memcachecient.add (key,3600, value); } @Override Public voidRemove (String key,intmilliseconds)      {Memcachecient.delete (key); } @Override Public voidRemove (String key) {memcachecient.delete (key); } @Override Public voidUpdate (String key, Object value,intmilliseconds)      {Memcachecient.replace (key, milliseconds, value); } @Override Public voidUpdate (String key, Object value) {memcachecient.replace (key,3600, value); } @Override PublicObject get (String key) {returnMemcachecient.get (key); }  }  

Xmemcached

Official website: https://github.com/killme2008/xmemcached

Operation Guide: https://github.com/killme2008/xmemcached/wiki/Xmemcached%20%E4%B8%AD%E6%96%87%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97

Currently the most fire, and the time-length update, as of today are still updated.

Importjava.io.IOException; Importjava.util.concurrent.TimeoutException; ImportNet.rubyeye.xmemcached.utils.AddrUtil; Importnet.rubyeye.xmemcached.MemcachedClient; ImportNet.rubyeye.xmemcached.MemcachedClientBuilder; ImportNet.rubyeye.xmemcached.XMemcachedClientBuilder; Importnet.rubyeye.xmemcached.exception.MemcachedException;  Public classTestxmemcache { Public Static voidMain (string[] args) {Memcachedclientbuilder builder=NewXmemcachedclientbuilder (addrutil.getaddresses ("10.11.15.222:10000"));          Memcachedclient memcachedclient; Try{memcachedclient=Builder.build (); Memcachedclient.set ("Hello", 0, "hello,xmemcached"); String value= Memcachedclient.get ("Hello"); System.out.println ("Hello=" +value); Memcachedclient.delete ("Hello"); Value= Memcachedclient.get ("Hello"); System.out.println ("Hello=" +value); //Close memcached ClientMemcachedclient.shutdown (); } Catch(memcachedexception e) {System.err.println ("Memcachedclient Operation Fail");          E.printstacktrace (); } Catch(TimeoutException e) {System.err.println ("Memcachedclient Operation Timeout");          E.printstacktrace (); } Catch(interruptedexception e) {//Ignore}Catch(IOException e) {System.err.println ("Shutdown memcachedclient fail");            E.printstacktrace (); }        }    }  

Reference:

http://blog.csdn.net/xuke6677/article/details/38706501

http://blog.csdn.net/hardy008/article/details/37738901

http://blog.csdn.net/arui_email/article/details/8129400

http://blog.csdn.net/heiyueya/article/details/64441901

http://exceptioneye.iteye.com/blog/1927741

http://blog.csdn.net/hengyunabc/article/details/20735701 (policy processing after cache invalidation)

Several Java clients for memcached

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.