Using the Spymemcached client, the Wireshark catches the packet and sees the memcached command more clearly.
Let's start with a simple demo:
PackageSPYMC;Importjava.io.IOException;Importjava.net.InetSocketAddress;ImportNet.spy.memcached.CASResponse;ImportNet.spy.memcached.CASValue;Importnet.spy.memcached.MemcachedClient; Public classTESTSPYMC { Public Static voidMain (string[] args)throwsIOException {memcachedclient C=NewMemcachedclient (NewInetsocketaddress ("aliyun.9game.cn", 11211)); C.set ( "ABC", 3600, "Hello"); System.err.println (c.get ( "ABC")); //casvalue<object> vasvalue = c.gets ("abc"); //casresponse resp= C.cas ("abc", Vasvalue.getcas (), "pre"); //System.out.println (Resp.name ()); //resp= C.cas ("abc", Vasvalue.getcas (), "after"); //System.out.println (Resp.name ());C.shutdown (); }}
The result of the grab will see:
set ABC 0 3600 5helloget ABC storedvalue ABC 0 5helloEND
The pink represents the request, and the blue represents the response. Spymemcached said in the previous section thatspymemcached is non-blocking . So the get command is sent out before the SET command response is issued.
Then look at the gets and the CAs. Try to do the CAS operation two times:
PackageSPYMC;Importjava.io.IOException;Importjava.net.InetSocketAddress;ImportNet.spy.memcached.CASResponse;ImportNet.spy.memcached.CASValue;Importnet.spy.memcached.MemcachedClient; Public classTESTSPYMC { Public Static voidMain (string[] args)throwsIOException {memcachedclient C=NewMemcachedclient (NewInetsocketaddress ("aliyun.9game.cn", 11211)); //C.set ("abc", 3600, "Hello"); //System.err.println (C.get ("abc"));Casvalue<Object> Vasvalue =c.gets ("abc" )); Casresponse resp=C.cas ("abc", Vasvalue.getcas (), "Pre"); System.out.println (Resp.name ()); Resp=C.cas ("abc", Vasvalue.getcas (), "after"); System.out.println (Resp.name ()); C.shutdown (); }}
First, the output is:
Okexists
What the clutch sees is:
gets ABC VALUE ABC 0 3 2272929preEND CAS ABC 0 0 3 2272929pre STORED CAS ABC 0 0 5 2272929after EXISTS
Gets and CAs use version number to achieve atomic operation, repeat two CAs, the same version number, the second time is a failure .
Then try to do a set operation between get and sets. Java code:
Packageegroup.spymctest;Importjava.io.IOException;Importjava.net.InetSocketAddress;ImportNet.spy.memcached.CASResponse;ImportNet.spy.memcached.CASValue;Importnet.spy.memcached.MemcachedClient; Public classTESTSPYMC { Public Static voidMain (string[] args)throwsIOException {memcachedclient C=NewMemcachedclient (NewInetsocketaddress ("aliyun.9game.cn", 11211));Casvalue<Object> Vasvalue =c.gets ("abc" )); C.set ( "ABC", 3600, "Hello SB"); Casresponse resp=C.cas ("abc", Vasvalue.getcas (), "Pre"); System.out.println (Resp.name ());C.shutdown (); }}
IDE Console output:
EXISTS
The grab bag concludes:
gets ABC VALUE ABC 0 8 2279149hello sbend set ABC 0 3600 8hello sbcas ABC 0 0 3 2279149pre storedexists
When set succeeds, CAS fails.
Visible, Gets&cas, is the concept of optimistic locking. Are not ' exclusive ' data.
Finally, try repeating the add operation. Java code:
C.add ("Addtest", 3600, "Hello"); C.add ("Addtest", 3600, "Hello");
The grab bag concludes:
Add addtest 0 3600 5helloadd addtest 0 3600 5hello STOREDnot_stored
The add command of the MC, if there is a key exists, the subsequent add operation will fail . A single point of memcached words can be used as a mutex to play.
Here are some summaries of the instructions:
- Gets &cas should appear in pairs. The version number of a numeric type is obtained in the Get, which is set when CAS operates. But! If you change this number between get and CAS, CAS will fail.
- The add operation cannot set duplicate key, otherwise it will fail.
Memcached (vii)--common instruction grasping packet analysis