Memcached client program
There are three of memcached Java clients:
* Official-provided client based on traditional blocking IO maintained by Greg Whalin
* spymemcached based on Java NIO implemented by dustinsallings
* xmemcached
1. Three kinds of API comparisons
1) memcached Client Forjava
The Memcached Java Client API, which was introduced earlier, is widely used and stable in operation.
2) spymemcached
A simple,asynchronous, single-threaded memcached client written in Java. Support asynchronous, single-threaded memcached clients, using the java1.5 version of concurrent and NIO, access speed will be higher than the former, but the stability is not good, the test often reported timeout and other related exceptions.
3) xmemcached
Xmemcached is also a client based on Java NIO, 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 in performance, in some aspects (the stored data is relatively small) xmemcached than the spymemcached performance is more excellent, can see this Java Memcached Clients Benchmark.
2. Recommends
Because memcached Client for Java has released a new version, has improved performance and is running stably, it is recommended that you use memcached clientfor java.
Xmemcached is also widely used, and has more detailed Chinese API documents, with the following characteristics: High-performance, support the full protocol, support client distribution, allow the setting of node weights, dynamic additions and deletions, support JMX, Integration with spring framework and hibernate-memcached, client connection pooling, scalability, and so on.
The sample programs for these three types of clients are given below.
3. Sample Programs
1) memcached Client Forjava
Download the latest version of the client package from the Memcached client program Web site in the Java environment described earlier: Java_memcached-release_2.5.1.zip, after decompression, find Java_ in the folder Memcached-release_2.5.1.jar, this is the client jar package. Add this jar package to your project's build path, and you can use memcached in your project.
The sample code is as follows:
Package temp;
importcom.danga.memcached.*;
importorg.apache.log4j.*;
Public Classcachetest {
public static void Main (string[] args) {
/**
* Initialize Sockiopool, manage memcached connection pool
* */
String[] Servers = {"10.11.15.222:10000"};
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 < 1000; i++) {
/**
* Adding objects to the memcached cache
* */
Boolean success = Memcachedclient.set ("" + I, "hello!");
/**
* object is 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));
}
}
}
2) spymemcached
Spymemcached Current version is 2.5 version, the official website is: http://code.google.com/p/spymemcached/. You can download the latest version from address: Http://spymemcached.googlecode.com/files/memcached-2.5.jar to use.
The sample code is as follows:
Package temp;
importjava.net.InetSocketAddress;
Importjava.util.concurrent.Future;
Importnet.spy.memcached.MemcachedClient;
Public Classtestspymemcache {
public static void Main (string[] args) {
Save Object
try {
/* Establish a Memcachedclient instance and specify the IP address and port number of the memcached service.
memcachedclient mc = new Memcachedclient (newinetsocketaddress ("10.11.15.222", 10000));
Future<boolean> B = null;
/* Set key value, Expiration Time (SEC) and object to be cached to memcached.
b = Mc.set ("Neea:testDaF:ksIdno", 900, "someobject");
if (B.get (). Booleanvalue () = = True) {
Mc.shutdown ();
}
catch (Exception ex) {
Ex.printstacktrace ();
}
Get objects
try {
/* Establish a Memcachedclient instance and specify the IP address and port number of the memcached service.
memcachedclient mc = new Memcachedclient (newinetsocketaddress ("10.11.15.222", 10000));
/* Find cache from memcached by key value, return NULL if not present * * *
Object B = Mc.get ("Neea:testDaF:ksIdno");
System.out.println (B.tostring ());
Mc.shutdown ();
catch (Exception ex) {
Ex.printstacktrace ();
}
}
}
3) xmemcached
Xmemcached's official web site is: http://code.google.com/p/xmemcached/, you can download the latest version of the 1.2 4来 from its official web use. The address is: http://xmemcached.googlecode.com/files/xmemcached-1.2.4-src.tar.gz.
The sample code is as follows:
Package temp;
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 void Main (string[] args) {
Memcachedclientbuilder builder = new Xmemcachedclientbuilder (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 Client
Memcachedclient.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 ();
}
}
}