Comparison and selection of three Java clients in Memcache __java

Source: Internet
Author: User
Tags cas memcached

Memcache as a well-known caching project to support multiple languages and multiple forms of clients, as a Java developer, we have to understand the different types of clients, which will help us in the selection of technology to fully assess the pros and cons, so as to select the best caching products and caching clients, Finally, make a reasonable decision on the technology selection.

1. Clients memcached Client for Java, which is maintained by Greg Whalin based on traditional blocking IO

URL: http://www.whalin.com/memcached
Latest Version: java_memcached-release_2.0.1
Features: The memcached Java Client API, which has been introduced earlier, is widely used and stable in operation, with blocking IO and does not support CAS operation.
Use examples:

    Import com.danga.memcached.*;  
    Import org.apache.log4j.*; public class Testmemcached {has public static void main (S have ing[] args) {/* initialization sockiopool, managing memcached's connection  
            Sink/string[] Servers = {"192.168.1.20:12111"};  
            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 ();  
            /* Establish memcachedclient instance * * memcachedclient memcachedclient = new Memcachedclient (); for (int i = 0; i < i++) {/* Add object to memcached Cache/Boolean success = Memcachedcli  
                Ent.set ("" + I, "hello!"); /* objects are taken from the memcached cache by key value */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));  
 }  
        }  
    }

Spymemcached based on Java NIO implemented by 2.Dustin Sallings

URL: http://code.google.com/p/spymemcached/
Latest Version: Memcached-2.1.jar
Features: A simple, asynchronous, single-threaded memcached-client written in Java. Using the java1.5 version of concurrent and NIO, the access rate will be higher than the former, but the stability is not good, the test often reported timeout and other related exceptions, support CAS operations.
Action Example:

To cache an object in spymemcached:

    Import java.net.InetSocketAddress;  
    Import Java.util.concurrent.Future;  
      
    Import net.spy.memcached.MemcachedClient;  
      
    The public class Mclient {public  
          
        static void Main (string[] args) {  
            try{/  
                * establishes the memcachedclient instance. and specify the IP address and port number of the memcached service * *  
                memcachedclient mc = new Memcachedclient (New Inetsocketaddress ("192.168.1.20", 12111) ;  
                Future B = null;  
                /* The key value, the Expiration Time (SEC) and the object to be cached set into memcached  
                /b = Mc.set ("Neea:testDaF:ksIdno", 900, "someobject");  
                if (B.get (). Booleanvalue () ==true) {  
                    mc.shutdown ();  
                }  
            }  
            catch (Exception ex) {  
                ex.printstacktrace ();}}}  
      

To obtain an object from the cache using spymemcached:

    Import java.net.InetSocketAddress;  
    Import Java.util.concurrent.Future;  
      
    Import net.spy.memcached.MemcachedClient;  
      
    The public class Mclient {public  
          
        static void Main (string[] args) {  
            try{/  
                * establishes the memcachedclient instance. and specify the IP address and port number of the memcached service * *  
                memcachedclient mc = new Memcachedclient (New Inetsocketaddress ("192.168.1.20", 12111) ;  
                /* To find the cache from the memcached according to the key value, and return null  
    /Object B = Mc.get ("Neea:testDaF:ksIdno") if it does not exist;  
                Mc.shutdown ();  
            }  
            catch (Exception ex) {  
                ex.printstacktrace ();}}}  
      

3 xmemcached

Xmemcached is also a client based on Java NIO, and Java NiO has the advantage of being more efficient (especially in high concurrency) and less resource consumption compared to traditional blocking IO models. Traditional blocking IO requires creating a certain number of connections to form a connection pool to improve efficiency, and NIO requires only one connection (and NiO, of course, can do the pooling), which reduces the overhead of thread creation and switching, which is particularly noticeable in high concurrency. So xmemcached and spymemcached are very good at performance, and in some ways (in the case of small storage data) xmemcached is better than spymemcached. Summary: Java Memcache Client for Java application is the most extensive, is currently the most stable client, online system a large number of users. Of course, Spymemecache and Xmemcache because of the use of NIO IO model, the performance is higher, stability is also improving, in the case of high performance requirements is also a good choice.

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.