memcached Introduction
Here is a brief introduction to memcached.
1, what is memcached
Memcached is an open source high-performance, distributed memory object caching system, through the form of key-value team to access the data, memcached is simple and powerful, its simple design to promote rapid deployment, easy to develop, solve the large data cache face many problems.
The official web site is: http://memcached.org/, there are already many well-known Internet applications used in memcached, such as Wikipedia, Flickr, Youtube, WordPress and so on.
2, download the memcached under the Windows platform, the address is:
Http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip
The corresponding source address is:
Http://code.jellycan.com/files/memcached-1.2.6-win32-src.zip
Then, unzip it, you will see a memcached.exe file, and install it as a system service to the machine
Then look at the system services and you'll see that the memcached service is already visible
Then, select the right mouse button for this service to start the service.
Enter in the DOS interface: Telnet 127.0.0.1 11211来 Verify that the service is starting correctly, and if it is correct, the following window will appear:
The error shown in the above figure is that I enter the character and then press ENTER, this is because you need to install the memcached specified protocol to enter, otherwise it will appear as shown on the error.
3, memcached protocol and data access
The so-called protocol, which can be understood as the syntax rules for its operation (data access), commonly used commands and parameters for accessing data are as follows:
Set: Depositing a record
Key: The key value of the record
Flags: a decimal int that identifies the client flag when the record is stored and returns when the record is taken out.
Exptim: Data expiration time, 0 means no expiration, other values are valid milliseconds, after expiration, the client will not be able to take this record, memcached in the expiration of the record will be emptied or deleted.
Get: Indicates the value of the key that is fetched from the memcached, and returns the end flag if there is no corresponding value
Append: Represents the value corresponding to the key and adds the input at the end
Delete: Remove Key's corresponding value
More protocols can be consulted: the Protocol.txt in the memcached package
Specific examples are as follows:
It is important to note that if you specify a character length of 5 at set time and the input exceeds this length, you will be given an error: Client_error bad data chunk
4. Write code to memcached data access operation
In general, you can use the open source has encapsulated the memcached client to operate the memcached, of course, you can also according to memcached protocol in the code by writing socket communication program implementation.
Download page of Memcached-java-client:
Http://github.com/gwhalin/Memcached-Java-Client/downloads, then select Download:
Java_memcached-release_2.5.1.zip
You can see some of the written examples in the Unpacked test directory, which can be done by running Com.danga.MemCached.test. Testmemcached to see the data in and out of the situation, and here also posted its code:
Package com.danga.MemCached.test;
Import com.danga.MemCached.MemCachedClient;
Import Com.danga.MemCached.SockIOPool;
Import org.apache.log4j.*; public class Testmemcached {public static void main (string[] args) {//memcached should is running on port 11211 but N
OT on 11212 basicconfigurator.configure ();
Cache server address, multiple servers are separated by commas, 11211 for memcached use of the port number string[] Servers = {"Localhost:11211″};"
Get a link pool object and do some initialization work 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 ();
Memcachedclient MCC = new Memcachedclient (); Turn off most memcached client logging://logger.getlogger (MemCachedClient.class.getName ()). Setlevel (Com.schooner.
Memcached.logger.); The following is an example of a data write and fetch operation for (int i = 0; i < i++) {Boolean success = Mcc.set("" + I, "hello!");
String result = (string) mcc.get ("" + i);
System.out.println (String.Format ("Set (%d):%s", I, success));
System.out.println (String.Format ("Get (%d):%s", I, result));
} System.out.println ("\n\t-sleeping–\n"); try {thread.sleep (10000);} catch (Exception ex) {} for (int i = 0; i < i++) {Boolean success = Mcc.set (
"" + I, "hello!");
String result = (string) mcc.get ("" + i);
System.out.println (String.Format ("Set (%d):%s", I, success));
System.out.println (String.Format ("Get (%d):%s", I, result));
}
}
}
Memcached Java Client Instance
Package com.danga.MemCached.test;
Import com.danga.memcached.*; public class Testmemcached {public static void main (string[] args) {/* Initialize Sockiopool, admin memcached connection Pool/string[] s
Ervers = {"192.168.105.217:11211"};
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 = Memcachedclient.set ("" + I, "hello!");
/* Object by key value from memcached cache/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));
}
}
}
1. Extract (extract to c:\memcached in this example).
2, in the command line state input: c:\memcached\memcached.exe-d install. This memcached has been installed as a Windows service
3, entered at the command line: c:\memcached\memcached.exe-d start to start the memcached service. Of course, you can also choose to start the
in Windows service