For some usage of memcached,
Memcached the version under Windows was very hard to find. Win32,win64 all have. Originally wanted to build their own, but Cygwin download package is also a fee for the old strength, not down.
On Linux can be used Rpm/yum direct installation, on the other hand, if you like build, then the official website has the source code, is also very convenient: http://memcached.org/
can also be found on GitHub: https://github.com/memcached/memcached
But I build the words memcached need libevent library, libevent: http://libevent.org/
Some of the daily use of Memcached syntax and ways to see:
Http://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html
Syntax format for the command:
<command> <key> <flags> <exptime> <bytes>
<value>
Explanatory notes:
<command> is the command to execute, such as set, append, prepend, etc.
<key> is like a key in the map, a key to find the keyword
<flags> client uses it to store additional information about key-value pairs
<exptime> This key value to the surviving time, the expiration is recycled, unit? (0 means never expire)
<bytes> indicates the number of bytes stored, (error if the number of bytes set is exceeded)
<value> similar to value in map, the values that are actually stored.
For example:
Set Username 0 0 8
Xiaobing
Information can be obtained through get
Get username.
The commonly used commands are:
Set
Add
Get
Gets
Delete
Replace
There is also a CAS command: CAS is the meaning of checked and set, which can only be stored if the last parameter matches the parameters obtained by the GET, otherwise "EXISTS" is returned.
Stats
Stats items
The results of the stats items execution are as follows:
The numbers in the red boxes are made of meaning, called slab_id,
Stats Cachedump slab_id Limit_num
SLAB_ID is the upper red box number, Limit_num is to query how many records out, 0 means query all.
Stats Slabs
Stats sizes
Stats Reset
Append backward Append
Prepend forward Append
Set Username 0 0 8
Xiaobing
Get username
The result is: xiaobing
Append username 0 0 5
Sheng
Get username
The result is: Xiaobingsheng
Prepend Username 0 0 5
Sheng
Get username
The result is: Shengxiaobingsheng
Flush_all
The command has an optional numeric parameter. It always executes successfully and the server sends a "ok\r\n" response. The effect is to invalidate an existing project immediately (the default), or after a specified time. After that the FETCH command is executed and nothing is returned (unless the same key name is re-stored). Flush_all actually does not immediately release the memory occupied by the project, but is executed later when new items are stored (this is determined by memcached's lazy detection and deletion mechanism).
The Flush_all effect is that it causes all items to be updated earlier than the time set by Flush_all, and the command is ignored when the FETCH command is executed.
4. Other commands
Memcached also has a lot of commands, such as the storage of digital type can be used by the INCR/DECR command to increase or decrease operations, and so on, here only the development and operation of the regular use of commands, the other no longer one by one examples.
Memcached Java Client This package can include:
<groupId>net.spy</groupId>
<artifactId>spymemcached</artifactId>
<packaging>jar</packaging>
<version>2.12.0</version>
How to use:
- Public static void Main (string[] args) throws IOException {
- Memcachedclient cache = New Memcachedclient (new Inetsocketaddress ("127.0.0.1", 11211));
- For (int i = 1; i < i++) {
- Cache.set ("T0001" + I, 3600, new User (i + ""));
- }
- User MyObject = (user) Cache.get ("T00011");
- System.out.println ("Get object from mem:" + myObject);
- }
Memcached Common configuration at startup:
First, the basic parameters
When we first installed memcached, this command was generally used:
I start the local only using the-p 11211-l localhost two parameters, without adding log ah, more detailed parameters can be-h
Memcached-win32-1.4.4-14>memcached.exe-p 11211-l localhost
Let's explain the meaning of these parameters first.
How to connect memcached: Use the telnet command:
Telnet Localhot 11211
So you can, after practicing well, you can first stats look at the state Oh.
-m specifies the maximum amount of memory used by the cache in megabytes, which is 64MB by default
-u Specifies this parameter only when run as root
-D runs in daemon form
-l Specifies the address of the listener
-p Specifies the TCP port number to listen on, default is 11211
Second, the other commonly used parameters
-t specifies the number of threads, the default is 4
-H Printing Help information
-c Maximum number of simultaneous connections, default is 1024.
-u Specifies the UDP port number to listen on, default is 11211
-M displays an error when running out of memory instead of deleting an item
The first "-D" parameter needs further explanation.
-D Install installation memcached
-D Uninstall Uninstall memcached
-D Start memcached service
-D Restart Restart memcached service
-D Stop Stop memcached service
-D Shutdown Stop memcached service
http://blog.csdn.net/wxwzy738/article/details/23703635
1. What is the CAS protocol
A lot of Chinese information will not tell you what is the full name of CAs, but be sure not to take CAS as an abbreviation of the Chinese Academy of Sciences (Academy of Sciences). Google.com, what is CAS? CAS is the abbreviation for check and set.
2. Original CAS protocol
Http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
3. The fundamentals of CAs
The basic principle is very simple, word, is the "version number." Each stored data object has more than one version number. We can understand from the following examples:
If you do not use CAs, you have the following scenario:
The first step, a takes out the data object X;
In the second step, B takes out the data object X;
In the third step, B modifies the data object X and puts it into the cache;
Fourth, a modifies the data object X and puts it into the cache.
We can see that the fourth step results in a data write conflict.
If the CAS protocol is used, it is the following scenario.
The first step, a takes out the data object X, and obtains to the CAS-ID1;
In the second step, B takes out the data object X and gets to the CAS-ID2;
In the third step, B modifies the data object X, before writing the cache, to check whether the Cas-id is consistent with the cas-id of the data in the cache space. The result is "consistent", which writes the modified X with Cas-id2 to the cache.
Fourth, a modifies the data object Y, before writing the cache, to check whether the Cas-id is consistent with the cas-id of the data in the cache space. The result is "inconsistent", the write is rejected, and the storage failure is returned.
So the CAS protocol uses the idea of "version number" to solve the conflict problem.
1. Non-CAs
First, look at a memcached program instance that is not a CAs. Example of a problem prototype, see the previous blog post.
Program Examples:
Package com.sinosuperman.memcached;
Import java.io.IOException;
Import java.net.InetSocketAddress;
Import net.spy.memcached.MemcachedClient;
Public class Test {
Public static void Main (string[] args) throws IOException {
Memcachedclient cache = New Memcachedclient (new Inetsocketaddress ("127.0.0.1", 11211));
Cache.set ("x", 1800, "Love");
String obj1 = (string) cache.get ("x");
String obj2 = (string) cache.get ("x");
Obj2 = "Michael";
Cache.set ("x", 1800, OBJ2);
System.out.println ("Non-cas 2:\t" + obj2);
System.out.println ("Non-cas 1:\t" + obj1);
}
}
Operation Result:
2011-12-18 23:12:39.836 INFO net.spy.memcached.MemcachedConnection:Added {QA sa=/127.0.0.1:11211, #Rops =0, #Wops = 0, # Iq=0, Toprop=null, Topwop=null, towrite=0, interested=0} to connect queue
2011-12-18 23:12:39.843 INFO net.spy.memcached.MemcachedConnection:Connection state changed for [email protected]
Non-cas 2:michael
Non-cas 1:love
Visible in multiple client operations, it is bound to cause the problem of write inconsistencies.
2. CAS
Program Examples:
Package com.sinosuperman.memcached;
Import java.io.IOException;
Import java.net.InetSocketAddress;
Import Net.spy.memcached.CASValue;
Import net.spy.memcached.MemcachedClient;
Public class Test {
@SuppressWarnings ("unchecked")
Public static void Main (string[] args) throws IOException {
Memcachedclient cache = New Memcachedclient (new Inetsocketaddress ("127.0.0.1", 11211));
Cache.set ("y", 1800, "Love");
Casvalue casValue1 = cache.gets ("Y");
Casvalue casValue2 = cache.gets ("Y");
Cache.cas ("Y", Casvalue2.getcas (), Casvalue2.getvalue ());
System.out.println ("CAS 2:\t" + Casvalue2.getcas ());
System.out.println ("Value 2:\t" + casvalue2.getvalue ());
System.out.println ("CAS 1:\t" + Casvalue1.getcas ());
System.out.println ("Value 1:\t" + casvalue1.getvalue ());
}
}
Operation Result:
2011-12-18 23:07:14.528 INFO net.spy.memcached.MemcachedConnection:Added {QA sa=/127.0.0.1:11211, #Rops =0, #Wops = 0, # Iq=0, Toprop=null, Topwop=null, towrite=0, interested=0} to connect queue
2011-12-18 23:07:14.541 INFO net.spy.memcached.MemcachedConnection:Connection state changed for [email protected]
CAS 2:11
Value 2:love
CAS 1:11
Value 1:love
Here is an introduction to memcached: http://os.51cto.com/art/201205/335034_all.htm
Memcached and Spring Integration:
<bean id= "memcachedclient" class= "Net.spy.memcached.spring.memcachedclientfactorybean" >
<property name= "Servers" value= "10.1.1.116:12000,10.1.1.116:12001,10.1.1.116:12002,10.1.1.116:12003"/>
<property name= "protocol" value= "binary"/>
<property name= "Transcoder" >
<bean class= "Net.spy.memcached.transcoders.serializingtranscoder" >
<property name= "Compressionthreshold" value= "1024x768"/>
</bean>
</property>
<property name= "optimeout" value= "/>"
<property name= "Timeoutexceptionthreshold" value= "1998"/>
<property name= "HashAlg" >
<value type= "Net.spy.memcached.defaulthashalgorithm" >ketama_hash</value>
</property>
<property name= "Locatortype" value= "consistent"/>
<property name= "Failuremode" value= "Redistribute"/>
<property name= "Usenaglealgorithm" value= "false"/>
</bean>
Some uses of Memcached