Introduction to Memcached
Let’s introduce Memcached below.
1. What is Memcached
Memcached is an open source high-performance, distributed memory object cache system that stores and accesses data through key-value teams. Memcached is simple and powerful. Its simple design promotes rapid deployment and is easy to develop, and solves many problems faced by big data cache.
The official website is: http://memcached.org/. Currently, many well-known Internet applications have used Memcached, such as Wikipedia, Flickr, Youtube, Wordpress, etc.
2. Download MemCached on Windows platform, the address is:
http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip
The corresponding source code address is:
http://code.jellycan.com/files/memcached-1.2.6-win32-src.zip
Then, unzip it and you will see a memcached.exe file, install it in the following figure, and install it on the machine in the form of system services.
Then check the system service and you will find that you can already see the memcached service
Then, right-click this service to start this service.
Enter: telnet 127.0.0.1 11211 in the DOS interface to confirm whether the service is started correctly. If it is correct, the following window will be displayed:
The ERROR shown in the picture above is displayed by me randomly entering the characters and pressing Enter. This is because you need to install the protocol specified by memcached to enter, otherwise the error as shown above will be displayed.
3. Memcached protocol and data access
The so-called protocol can be understood as the syntax rules for its operations (data access). Common commands and parameters for accessing data are as follows:
set: Save a record
key: the recorded key value
flags: decimal int, the client flag that identifies the record when storing records, and will be returned when the record is retrieved.
exptim: The expiration time of the data, 0 means expired, and other values represent valid milliseconds. After the expiration, the client will not be able to retrieve this record, and the expiration record in memcached will be cleared or deleted.
get: means to take out the corresponding value of the key from memcached. If there is no corresponding value, the end flag END will be returned.
append: means that the input content is added to the value corresponding to the key at the end
delete: delete the value corresponding to the key
For more protocols, please refer to: protocol.txt brought in the memcached package
Specific examples are:
It should be noted that if the specified character length is 5 when set and the input content exceeds this length, an error will be reported: CLIENT_ERROR bad data chunk
4. Write code to access data on memcached
Generally speaking, you can use the open source encapsulated memcached client to operate on memcached. Of course, you can also implement it in the code by writing socket communication programs according to the memcached protocol.
Memcached-Java-Client download page:
http://github.com/gwhalin/Memcached-Java-Client/downloads and select Download:
java_memcached-release_2.5.1.zip
You can see some well-written examples in the unzipped Test directory. You can check the data storage and withdrawal status by running com.danga.MemCached.test. TestMemcached. The code is also posted here:
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 be running on port 11211 but NOT on 11212BasicConfigurator.configure();// cache server address, multiple servers are separated by commas, 11211 is the port number used by memcached 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 data writing and fetching operations for ( int i = 0; i < 10; 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 < 10; 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's java client instance
package com.danga.MemCached.test; import com.danga.MemCached.*; public class TestMemcached { public static void main(String[] args) { /*Initialize SockIOPool and manage memcached connection pool*/ String[] servers = { "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(); /*Create MemcachedClient instance*/ MemCachedClient memCachedClient = new MemCachedClient(); for (int i = 0; i < 10; i++) { /*Add object to memcached cache*/ boolean success = memCachedClient.set("" + i, "Hello!"); /*Fetch the object by key value from the 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. Decompress (in this example, decompress to c:/memcached).
2. Enter in the command line state: c:/memcached/memcached.exe -d install . So far, memcached has been installed into a Windows service
3. Enter: c:/memcached/memcached.exe -d start to start the memcached service. Of course, you can also choose to start in the Windows service