1. Download and install
1. Download memcache's windows stable version from the http://jehiah.cz/projects/memcached-win32/, unzip it under a disk, such as in F: \ memcached
2. Enter 'f: \ memcached \ memcached.exe-D install' on the terminal (that is, the CMD command interface) to install
3. Enter 'f: \ memcached \ memcached.exe-D start' to start
4. Download the development jar package:
Memcached client for Java: https://github.com/gwhalin/Memcached-Java-Client/downloads
Spymemcached: http://code.google.com/p/spymemcached/
Xmemcached: http://code.google.com/p/xmemcached/ --- Note: Later memcached will be used as a Windows Service and will be automatically started every time it is turned on. Default port: 11211.
2. Three memcahe APIs
(1) memcached client for Java, the earliest Java client API launched by memcache, is constantly updated, and the performance is improved and stable.
(2) spymemcached
A simple, asynchronous, single-thread memcached client written in Java. supports asynchronous single-thread memcached clients.
Using NiO and concurrent that jdk1.5 has heard, the access speed is higher than the former, but the stability is poor. A timeout exception is often reported in the test.
(3) xmemcached
Xmemcached is also a client based on Java NiO. Compared with traditional Io, Java NiO has the advantages of high efficiency (especially in processing concurrency) and low resource consumption.
Traditional Io requires the establishment of multiple connections to form a connection pool, while NiO only needs one connection (NIO can also be pooled), reducing the thread creation and switching overhead, which is highly concurrent.
Especially obvious. Therefore, xmemcached and spymemcached have excellent performance. In some ways (when the storage data volume is small), xmemcached needs
Better than spymemcached.
3. program reference example-see Java Test File
Package COM. boonya. mecache; import COM. danga. memcached. memcachedclient; import COM. danga. memcached. sockiopool; public class testmemcached {/*** @ Param ARGs */public static void main (string [] ARGs) {/*** initialize sockiopool, manage the memcached thread pool */string servers [] = {"192.168.20.37: 11211"}; sockiopool pool = sockiopool. getinstance (); pool. setservers (servers); pool. setfailover (true); pool. setinitconn (10); pool. setminconn (5); pool. set maxconn (300); pool. setmaintsleep (30); pool. setnagle (false); pool. setsocketto (3000); pool. setalivecheck (true); pool. initialize ();/*** create a memcachedclient instance */memcachedclient memcacheclient = new memcachedclient (); For (INT I = 0; I <100; I ++) {Boolean success = memcacheclient. set ("key" + I, "a_memcached"); // Add the object to the memcache Cache/*** read the memcache cache */string result = (string) memcacheclient. get ("key" + I); system. out. println (string. format ("set {% d }:% s", I, success); system. out. println (string. format ("Get {% d }:% s", I, result) ;}} package COM. boonya. mecache; import Java. io. ioexception; import java.net. inetsocketaddress; import Java. util. concurrent. executionexception; import Java. util. concurrent. future; import net. spy. memcached. memcachedclient; public class testspymemcached {public static void main (string [] ARGs) throws interruptedexception, executionexception {/*** example of creating memcached */try {memcachedclient memcacheclient = new memcachedclient (New inetsocketaddress ("192.168.20.37", 11211); Future <Boolean> FBl = NULL; /* set the key and expiration time to memcache */FBl = memcacheclient. set ("User: Data: Key", 1000, "me"); If (FBl. get (). booleanvalue () = true) {memcacheclient. shutdown () ;}} catch (ioexception e) {e. printstacktrace ();}/*** get an example of memcached */try {memcachedclient memcacheclient = new memcachedclient (New inetsocketaddress ("192.168.20.37", 11211); object OBJ = memcacheclient. get ("User: Data: Key"); system. out. println (OBJ); memcacheclient. shutdown ();} catch (ioexception e) {e. printstacktrace () ;}} package COM. boonya. mecache; import Java. io. ioexception; import Java. util. concurrent. timeoutexception; import net. rubyeye. xmemcached. memcachedclient; import net. rubyeye. xmemcached. memcachedclientbuilder; import net. rubyeye. xmemcached. xmemcachedclientbuilder; import net. rubyeye. xmemcached. exception. memcachedexception; import net. rubyeye. xmemcached. utils. addrutil; public class testxmemcached {public static void main (string [] ARGs) {memcachedclientbuilder builder = new xmemcachedclientbuilder (addrutil. getaddresses ("192.168.20.37: 11211"); try {memcachedclient memcacheclient = builder. build (); try {memcacheclient. set ("key", 0, "data"); string value = memcacheclient. get ("key"); system. out. println ("key:" + value); memcacheclient. delete ("key"); value = memcacheclient. get ("key"); system. out. println ("key:" + value); memcacheclient. shutdown ();} catch (timeoutexception e) {e. printstacktrace ();} catch (interruptedexception e) {e. printstacktrace ();} catch (memcachedexception e) {e. printstacktrace () ;}} catch (ioexception e) {e. printstacktrace ();}}}
4. Usage suggestions
(1) memcached client for Java is constantly updated, and its performance is improved and stable.
(2) xmemcached is widely used and has the following features: high performance, support for complete protocols, support for client distribution, allow setting of node weights, dynamic addition and deletion of nodes, support for JMX, Spring framework and
Hibernate-memcached integration, client connection pool, and good scalability.