Memcached building a cache system I. Concept
Memcached is a set of distributed memory object caching systems developed by danga.com (the technical team of operations LiveJournal) to reduce database load and improve performance in dynamic systems.
Second, the application of the occasion
- Distributed applications. Because the memcached itself is based on a distributed system, it is particularly suitable for large distributed systems.
- Database pre-segment cache. Database is often the bottleneck of the website system. Large concurrent access to the database often results in Web site memory overflow. Of course we can also use Hibernate's caching mechanism. But Memcached is based on distributed, and can be independent of the website application itself, so it is more suitable for large-scale web site for the application of the split.
- Data sharing between servers. For example, we will be the site's login system, query system split into two applications, placed on different servers, and cluster, that time when the user logged on, how to log on the login information from the system server synchronization to the query system server? At this time, we can use memcached, login system to cache the login information, the query system can get login information, like to obtain local information.
Third, not applicable occasions
Those that do not need to be "distributed", do not need to be shared, or simply scale to a single server, memcached does not bring any benefit, and instead slows down system efficiency because network connectivity also requires resources
Four, installation
The installation of the Windows environment is described here.
- Download Memcache's Windows stable version and unzip it under a disk, for example, in c:\memcached
- Enter ' c:\memcached\memcached.exe-d install ' installation under CMD
- Re-enter: ' c:\memcached\memcached.exe-d start ' starts.
Memcached will start automatically every time a service is turned on for Windows. This way the server side is already installed.
V. Client
The memcached itself is developed using C, and the client can be PHP, C #, or Java. I do Java, so this is the only Java-based client.
I see a Java-based client on the Internet with two
1.java_memcached-release_2.6.3
1) Introduction
This is a more general-purpose memcached client framework. Specific original unknown.
2) Dependent jar
A.commons-pool-1.5.6.jar
B.java_memcached-release_2.6.3.jar
C.slf4j-api-1.6.1.jar
D.slf4j-simple-1.6.1.jar
2.alisoft-xplatform-asf-cache-2.5.1
1) Introduction
This stuff is the architect of Ali software Zenwen to encapsulate. The comments inside are all Chinese and better.
2) Dependent jar
A.alisoft-xplatform-asf-cache-2.5.1.jar
B.commons-logging-1.0.4.jar
C.hessian-3.0.1.jar
D.log4j-1.2.9.jar
E.stax-api-1.0.1.jar
F.wstx-asl-2.0.2.jar
Vi. Example 1. Based on java_memcached-release_2.6.3
PackageCom.hl.memcached.cache; Importjava.util.Date; Importcom.danga.MemCached.MemCachedClient; ImportCom.danga.MemCached.SockIOPool; Public classMycache { Public Static voidMain (string[] args) {memcachedclient client=Newmemcachedclient (); String [] Addr={"127.0.0.1:11211"}; Integer [] Weights= {3}; Sockiopool Pool=sockiopool.getinstance (); Pool.setservers (addr); Pool.setweights (weights); Pool.setinitconn (5); Pool.setminconn (5); Pool.setmaxconn (200); Pool.setmaxidle (1000*30*30); Pool.setmaintsleep (30); Pool.setnagle (false); Pool.setsocketto (30); Pool.setsocketconnectto (0); Pool.initialize (); //String [] s =pool.getservers (); Client.setcompressenable (true); Client.setcompressthreshold (1000*1024); //Putting data into the cacheClient.set ("Test2", "Test2"); //put data into the cache and set the expiration timeDate date=NewDate (2000000); Client.set ("Test1", "Test1", date); //Delete cached Data//client.delete ("test1"); //Get Cached DataString str = (string) client.get ("Test1"); System.out.println (str); } }
2. Based on alisoft-xplatform-asf-cache-2.5.1 1) configuration Memcached.xml
<?XML version= "1.0" encoding= "UTF-8"?> <memcached> <!--The Name property is a unique identifier for using the cache in the program, and the Socketpool property is associated with the subsequent Socketpool configuration; - <Clientname= "Mclient_0"compressenable= "true"defaultencoding= "UTF-8"Socketpool= "Pool_0"> <!--optional, used to handle error conditions <errorhandler>com.alisoft.xplatform.asf.cache.memcached.memcachederrorhandler &L T;/errorhandler> - </Client> <!--The Name property is associated with the Socketpool property in the client configuration. The Maintsleep property is the check interval for the background thread management Socketio pool, and if set to 0, indicates that no background thread is required to maintain the socketio thread pool, which is managed by default. The Socketto property is the socket operation timeout configuration, in unit Ms. The Alivecheck property indicates whether the socket state is checked before using the socket. - <Socketpoolname= "Pool_0"Maintsleep= "the"Socketto= " the"Failover= "true"Alivecheck= "true"Initconn= "5"Minconn= "5"Maxconn= "+"Nagle= "false"> <!--set the Memcache server-side instance address. Multiple addresses separated by "," - <Servers>127.0.0.1:11211</Servers> <!--Optional Configuration. Indicates the load weight of the server instance set above. For example <weights>3,7</weights> represents 30% load at 10.2.224.36:33001, 70% load in 10.2.224.46:33001 <weights>3,7</weights> - </Socketpool> </memcached>
2) Test class
Packagecom.hl.memcached.client.test; Importjava.util.ArrayList; Importjava.util.List; ImportCom.alisoft.xplatform.asf.cache.ICacheManager; ImportCom.alisoft.xplatform.asf.cache.IMemcachedCache; ImportCom.alisoft.xplatform.asf.cache.memcached.CacheUtil; ImportCom.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager; ImportCom.hl.memcached.cache.client.TestBean; Public classclienttest {@SuppressWarnings ("Unchecked") Public Static voidMain (string[] args) {Icachemanager<IMemcachedCache>Manager; Manager= Cacheutil.getcachemanager (Imemcachedcache.class, Memcachedcachemanager.class. GetName ()); Manager.setconfigfile ("Memcached.xml"); Manager.start (); Try{Imemcachedcache Cache= Manager.getcache ("Mclient_0"); Cache.put ("Key", "value"); System.out.println (Cache.get ("Key")); } finally{manager.stop (); } } }
Vii. using memcached to cache Java Bean Custom objects
Memcached can cache a string or cache a custom Java bean. But it must be a serializable Java bean (implements serializable)
- Based on java_memcached-release_2.6.3
Java Beans for testing
package com.hl.memcached.cache.client; import java.io.Serializable; public class Testbean implements serializable{ private static final long serialversionuid = 5344571864700659321l; private String name; private Integer age; // get, set method slightly }
Mycache.java Code
Importjava.util.Date; Importcom.danga.MemCached.MemCachedClient; ImportCom.danga.MemCached.SockIOPool; Public classMycache { Public Static voidMain (string[] args) {memcachedclient client=Newmemcachedclient (); String [] Addr={"127.0.0.1:11211"}; Integer [] Weights= {3}; Sockiopool Pool=sockiopool.getinstance (); Pool.setservers (addr); Pool.setweights (weights); Pool.setinitconn (5); Pool.setminconn (5); Pool.setmaxconn (200); Pool.setmaxidle (1000*30*30); Pool.setmaintsleep (30); Pool.setnagle (false); Pool.setsocketto (30); Pool.setsocketconnectto (0); Pool.initialize (); //String [] s =pool.getservers (); Client.setcompressenable (true); Client.setcompressthreshold (1000*1024); //Putting data into the cacheTestbean bean=NewTestbean (); Bean.setname ("Name1"); Bean.setage (25); Client.add ("Bean1", Bean); //Get Cached DataTestbean beanclient= (Testbean) client.get ("Bean1"); System.out.println (Beanclient.getname ()); } }
2. Based on alisoft-xplatform-asf-cache-2.5.1
Importjava.util.ArrayList; Importjava.util.List; ImportCom.alisoft.xplatform.asf.cache.ICacheManager; ImportCom.alisoft.xplatform.asf.cache.IMemcachedCache; ImportCom.alisoft.xplatform.asf.cache.memcached.CacheUtil; ImportCom.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager; ImportCom.hl.memcached.cache.client.TestBean; Public classclienttest {@SuppressWarnings ("Unchecked") Public Static voidMain (string[] args) {Icachemanager<IMemcachedCache>Manager; Manager= Cacheutil.getcachemanager (Imemcachedcache.class, Memcachedcachemanager.class. GetName ()); Manager.setconfigfile ("Memcached.xml"); Manager.start (); Try{Imemcachedcache Cache= Manager.getcache ("Mclient_0"); Testbean Bean=NewTestbean (); Bean.setname ("Name1"); Bean.setage (25); Cache.put ("Bean", Bean); Testbean beanclient= (Testbean) cache.get ("Bean"); System.out.println (Beanclient.getname ()); List<TestBean> list=NewArraylist<testbean>(); List.add (Bean); Cache.put ("Beanlist", list); List<TestBean> listclient= (list<testbean>) cache.get ("Beanlist"); if(Listclient.size () >0) {Testbean bean4list=listclient.get (0); System.out.println (Bean4list.getname ()); } } finally{manager.stop (); } } }
Viii. References:
http://blog.developers.api.sina.com.cn/?p=124
Http://www.infoq.com/cn/articles/memcached-java
Nine, source code and information see annex
- java_memcached-release_2.6.3 data. RAR (191.4 KB)
- Download number of times: 727
- ALISOFT-XPLATFORM-ASF-CACHE-2.5.1-SRC data. RAR (1.3 MB)
- Download number of times: 706
- Memcached-1.2.1-win32 installation files. RAR (82.6 KB)
- Download number of times: 526
- memcached-1.4.13 source code. GZ (313.2 KB)
- Download number of times: 492
- Demo.rar (1006.6 KB)
- Download number of times: 663
From: http://my249645546.iteye.com/blog/1420061
Installation and use of memcached under window