Reproduced in http://blog.csdn.net/xzknet/article/details/44066141
http://www.runoob.com/memcached/java-memcached.html Beginner Tutorial-Simple operation
Memcached client program
There are three types of memcached Java clients:
1 officially provided clients based on the traditional blocking IO maintained by Greg Whalin
2 Java NIO-based spymemcached implemented by Dustin Sallings
3 xmemcached
1. Three different API comparisons
1) memcached Client for Java
The Memcached Java Client API, which has been introduced earlier, is widely used and runs more stably.
2) 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.
3) xmemcached
Xmemcached is also a Java NIO-based client, and Java NiO has the advantage of being more efficient (especially at high concurrency) and less resource-intensive than the traditional blocking IO model. 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.
2. Recommendations
memcached Client for Java is recommended because memcached Client for Java publishes a new version, improves performance and runs stably.
Xmemcached is also widely used, and has a more detailed Chinese API documentation, with the following features: High performance, support for a complete protocol, support client distribution, allow to set node weights, dynamic addition and deletion of nodes, support JMX, Integration with spring framework and hibernate-memcached, client connection pooling, scalability, and more.
Here are the sample programs for these three types of clients.
3. Sample Programs
1) memcached Client for Java
From the memcached client program URL of the Java environment described earlier, download the latest version of the client package: Java_memcached-release_2.5.1.zip, unzip, find the folder java_memcached- Release_2.5.1.jar, this is the client's jar package. Add this jar package to the project's build path, and you can use memcached in your project.
The sample code is as follows:
Package temp;
Import com.danga.memcached.*;
Import org.apache.log4j.*;
public class Cachetest {
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 instances
* */
Memcachedclient memcachedclient = new Memcachedclient ();
for (int i = 0; i <; i++) {
/**
* Adding objects to the memcached cache
* */
Boolean success = Memcachedclient.set ("" + I, "hello!");
/**
* object is taken from the memcached cache by the 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 version 2.5, 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;
Import java.net.InetSocketAddress;
Import Java.util.concurrent.Future;
Import net.spy.memcached.MemcachedClient;
public class Testspymemcache {
public static void Main (string[] args) {
Save Object
try {
/* Establish an Memcachedclient instance and specify the IP address and port number of the memcached service */
memcachedclient mc = new Memcachedclient (New Inetsocketaddress ("10.11.15.222", 10000));
Future<boolean> B = null;
/* Set the key value, the expiration time (in seconds), and the object to be cached to memcached */
b = Mc.set ("Neea:testDaF:ksIdno", "someobject");
if (B.get (). Booleanvalue () = = True) {
Mc.shutdown ();
}
} catch (Exception ex) {
Ex.printstacktrace ();
}
Get Object
try {
/* Establish an Memcachedclient instance and specify the IP address and port number of the memcached service */
memcachedclient mc = new Memcachedclient (New Inetsocketaddress ("10.11.15.222", 10000));
/* Find the cache from memcached by key value and 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 website is: http://code.google.com/p/xmemcached/, which can download the latest version of 1.2 from its official website. 4来 use. The address is: http://xmemcached.googlecode.com/files/xmemcached-1.2.4-src.tar.gz.
The sample code is as follows:
Package temp;
Import java.io.IOException;
Import java.util.concurrent.TimeoutException;
Import Net.rubyeye.xmemcached.utils.AddrUtil;
Import net.rubyeye.xmemcached.MemcachedClient;
Import Net.rubyeye.xmemcached.MemcachedClientBuilder;
Import Net.rubyeye.xmemcached.XMemcachedClientBuilder;
Import net.rubyeye.xmemcached.exception.MemcachedException;
public class Testxmemcache {
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 ();
}
}
}
The principle and implementation of memcache in Java development