After learning to use java_memcache operation Memcache , interested in the study of java_memcache source code. Java_memcache encapsulates the method of data manipulation in class asciiclient set/add/delete/append/get and so on.
Store Data set
Explained by the Memcache command, we know that the Memcache original set command format is
Set <key> <flag> <expiretime> <bytes> \ r \ n
<value> \ r \ n
In java_memcache , the set operation data is called Set (String, String, Object, Date, Integer, Long, Flag) method.
Private Boolean set (string s, string s1, Object obj, date date, integer integer, Long long1, Boolean flag) {SCHOONERSOC KIO Schoonersockio; int i; String S2; ... s1 = Sanitizekey (S1); ... Schoonersockio = Pool.getsock (S1, Integer); if (Schoonersockio = = null) {if (ErrorHandler! = null) Errorhandler.handleerroronset (this, new IOEXCEP tion ("No socket to server available"), S1); return false; if (date = = null) date = new Date (0L); i = Nativehandler.getmarkerflag (obj); S2 = s + "" + S1 + "" + i + "" + date.gettime ()/1000L + ""; Boolean Flag1; Schoonersockio.writeBuf.clear (); Schoonersockio.writeBuf.put (S2.getbytes ()); Int J = schoonersockio.writeBuf.position (); Schoonersockio.writeBuf.put (bland_data_size); if (Long1.longvalue ()! = 0L) schoonersockio.writeBuf.put ((New StringBuilder ()). Append (""). Append (Long1.tostring ()). ToString (). GetBytes ()); Schoonersockio.writeBuf.put (B_return); Sockoutputstream Sockoutputstream = new Sockoutputstream (Schoonersockio); int k = 0; if (i! = 0) {byte abyte0[]; if (flag) Abyte0 = Obj.tostring (). GetBytes (defaultencoding); else Abyte0 = Nativehandler.encode (obj); Sockoutputstream.write (ABYTE0); K = Abyte0.length; } else {k = Transcoder.encode (Sockoutputstream, obj); } schoonersockio.writeBuf.put (B_return); byte abyte1[] = (new Integer (k)). ToString (). GetBytes (); int L = schoonersockio.writeBuf.position (); Schoonersockio.writeBuf.position (j); Schoonersockio.writeBuf.put (ABYTE1); Schoonersockio.writeBuf.position (l); Schoonersockio.flush (); String s3 = (New Sockinputstream (Schoonersockio, 2147483647)). GetLine (); if (! " Stored\r\n ". Equals (S3)) break missing_block_label_538; ... return false;}
when we use memcacheclient.set ( "name","abcdef"), by comparing the above code, first i=nativehandler.getmarkerflag ( "abcdef") , and then stitch the string s2= "Set name 0"and put the S2 into bytebuffer , and then call theschoonersockio.writeBuf.put (bland_data_size),and bladn_data_size is " ". GetBytes ()isactually "set name 0 " and finally /c6>schoonersockio.writeBuf.put (b_return) is input \ r \ n, and finally Transcoder.encode () writes value to .
public void Encode (OutputStream outputstream, Object obj) throws ioexception{ ObjectOutputStream ObjectOutputStream = new ObjectOutputStream (outputstream); Objectoutputstream.writeobject (obj); Objectoutputstream.close ();}
is actually serializing the object, so the custom object that uses the Java_memcache operation must implement the Serializable interface.
However, if we run directly " Set name 0 \ r \ n " An error message is prompted.
I'm confused about how the final <byte> parameter of the set command is handled????
The anti-compilation code is not full or anti-compilation error?
data Extraction Get
Similarly, thememcache original get command is simple, and its basic format is:
Get <key> \ r \ n
In java_memcache ,theasciiclient is handled as follows:
Public Object get (String s) { return get (S, null);} Public Object Get (String S, integer integer) { return get ("get", S, Integer, false);}
Private Object Get (string s, String s1, Integer integer, Boolean flag) {Schoonersockio Schoonersockio; String S2; ..... S1 = Sanitizekey (S1); Schoonersockio = Pool.getsock (S1, Integer); S2 = (new StringBuilder ()). Append (s). Append (""). Append (S1). ToString (); int i; Int J; Sockinputstream Sockinputstream; Boolean Flag1; StringBuffer StringBuffer; int l; Schoonersockio.writeBuf.clear (); Schoonersockio.writeBuf.put (S2.getbytes ()); Schoonersockio.writeBuf.put (B_return); Schoonersockio.flush (); i = 0; j = 0; Sockinputstream = new Sockinputstream (Schoonersockio, 2147483647); Flag1 = false; StringBuffer = new StringBuffer (); L = 0; <span style= "color: #006600;" >//the previous section is the build string "get name \ r \ n", which is a GET command to Memcache server </span>_l5:int K; if (FLAG1) break missing_block_label_365; K = Sockinputstream.read (); if (k! = && k! =) break missing_block_label_353; <span style= "ColoR: #006600; " >//always read to carriage return or line feeds </span> l; JVM INSTR tableswitch 0 3:default 322 goto _l1 _l2 _l1 _l3 _l4 <span style= "color: #006600;" >//here is a while loop that receives message characters returned from Memcache server </span>_L1:break; /* Loop/switch isn ' t completed */_l2:object obj; if (! " End\r\n ". StartsWith (Stringbuffer.tostring ())) break; /* Loop/switch isn ' t completed */<span style= "color: #006600;" >//if it is not a end\r\r line, continue processing, or return null </span> obj = null directly; if (Schoonersockio! = null) {schoonersockio.close (); Schoonersockio = null; } return obj; <span style= "color: #006600;" >//here does not take any value when there is no corresponding key value in Memcache, call the Get command and get end/r/n directly, return empty </span>_l3:j = Integer.parseint ( Stringbuffer.tostring ()); Break /* Loop/switch isn ' t completed */_l4:i = Integer.parseint (stringbuffer.tostring ()); l++; StringBuffer = new StringBuffer (); if (k = =) {Sockinputstream.read (); Flag1 = true; } Goto _L5 STRINGBUFFER.APpend ((char) k); Goto _L5 Object obj3; Object obj1 = null; Sockinputstream.willread (i); if (i > 0) if (nativehandler.ishandled (j)) {byte abyte0[] = Sockinputstream.getbuffer (); if ((J & 2) = = 2) {Gzipinputstream Gzipinputstream = new Gzipinputstream (New ByteArray InputStream (ABYTE0)); Bytearrayoutputstream Bytearrayoutputstream = new Bytearrayoutputstream (abyte0.length); byte abyte1[] = new byte[2048]; int i1; while ((I1 = Gzipinputstream.read (abyte1))! =-1) bytearrayoutputstream.write (abyte1, 0, I1); Abyte0 = Bytearrayoutputstream.tobytearray (); <span style= "color: #006600;" >//reading data into byte array </span> gzipinputstream.close (); } if (Primitiveasstring | | flag) OBJ1 = new String (ABYTE0, defaultencoding); else Obj1 = NatiVehandler.decode (Abyte0, J); <span style= "color: #006600;" >//Last Call decodestring decode string </span>} else if (transcoder! = null) { Object obj2 = Sockinputstream; if ((J & 2) = = 2) obj2 = new Gzipinputstream (((Java.io.InputStream) (OBJ2)); if (ClassLoader = = null) Obj1 = Transcoder.decode (((Java.io.InputStream) (OBJ2)); else Obj1 = ((Objecttranscoder) transcoder). Decode (((Java.io.InputStream) (OBJ2)), ClassLoader); } sockinputstream.willread (2147483647); Sockinputstream.getline (); Sockinputstream.getline (); Obj3 = obj1; if (Schoonersockio! = null) {schoonersockio.close (); Schoonersockio = null; } return obj3;}
We can compare the input output directly using the memcache command operation, which is to parse the sequence of characters after entering the get command and encode the actual data.
Using the post-compilation code may be difficult to understand, and we can look at the Java_memcache client source code and learn more.
Some versions of the original code are directly get/set data in memcacheclient .
https://code.csdn.net/dwzteam/dwz_springmvc/tree/f744cf61e31188e93c31644686a07ece5b84e1e4/src/dwz/cache/ Memcache/client/memcachedclient.java
This Code Analysis Memcache processing data is much simpler.
Summary
At this point, we show you how toWindowsInstall and operateMemcache, the analysis ofMemcachedistributed principle, discusses the generalHashalgorithm and consistencyHashalgorithm and how to manipulate the data. We're on the whole.Java_memcache Clientwith a deep understanding, the main idea is to maintain a set of distributedServerthe information for eachServermaintain itsSocketconnected, according to the data of the OperationHashmap to a singleServer, and finally buildmemcachecommand to manipulate and parse data.
It is believed that you will use it more conveniently after mastering the java_memcache_client thought and the realization process.
Memcache combat in Windows environment http://blog.csdn.net/musa875643dn/article/details/45439417
Memcache Command Detailed http://blog.csdn.net/musa875643dn/article/details/45935895
Memcache Distributed principle Analysis http://blog.csdn.net/musa875643dn/article/details/45675711
Memcache Hash algorithm secret http://blog.csdn.net/musa875643dn/article/details/45796887
Java memcache Client Data operation Source Analysis