memcached-java-client-3.0.2 detailed
One:memcachedclientthe Creation
The creation of this client object is constructed in the following ways:
1,
/** * Creates a new instance of Memcachedclient. */ Public Memcachedclient () { This (null, True, false); } |
2,
/** * Creates a new instance of Memcachedclient accepting a passed in pool * Name. * * @param poolname * Name of Sockiopool */ Public memcachedclient (String poolname) { This (poolname, true, false); } |
3,
/** * Create memcached client. * * @param poolname * Name of Sockiopool * @param istcp * Use TCP protocol * @param binaryprotocal * Use binary protocol. */ Public Memcachedclient (String poolname, Boolean istcp, Boolean binaryprotocal) { if (binaryprotocal) Client = new Binaryclient (poolname); Else Client = istcp ? New asciiclient(poolname): New Asciiudpclient (poolname); } |
our usual method of construction is the two kinds, from the code, we really create the client object is mostly asciiclient.
The following are the inheritance relationships for their clients:
650) this.width=650; "title=" 11.png "src=" http://s3.51cto.com/wyfs02/M01/59/F6/wKioL1TxcwHD5cmHAACK4rBh6r8387.jpg "alt=" Wkiol1txcwhd5cmhaack4rbh6r8387.jpg "/>
in each of the sub-classes, the inside of the constructor will call the internal void init () method to initialize the state.
asciiclient of the Init The method is implemented as follows:
/** * Initializes client object to defaults. * * This enables compression and sets compression threshhold to KB. */ private void init () { This.sanitizekeys = true; Whether the//key value is encoded Urlencoder.encode this.primitiveasstring = false; this.compressenable = false; This.compressthreshold = Compress_thresh; this.defaultencoding = "UTF-8"; this.poolname = (This.poolname = = null)? "Default": This.poolname; Get a pool instance to work with for that life's this instance This.pool = schoonersockiopool.getinstance (poolname); } |
asciiudpclient of the Init The method is implemented as follows:
/** * Initializes client object to defaults. * * This enables compression and sets compression threshhold to KB. */ private void init () { This.sanitizekeys = true; This.primitiveasstring = false; This.compressenable = true; This.compressthreshold = Compress_thresh; this.defaultencoding = "UTF-8"; This.poolname = (This.poolname = = null)? "Default": This.poolname; Get a pool instance to work with for that life's this instance This.pool = Schoonersockiopool.getinstance (poolname); } |
binaryclient of the Init The method is implemented as follows:
/** * Initializes client object to defaults. * * This enables compression and sets compression threshhold to KB. */ private void init () { This.poolname = (This.poolname = = null)? "Default": This.poolname; Get a pool instance to work with for that life's this instance This.pool = Schoonersockiopool.getinstance (poolname); } |
Two: Common method analysis (inasciiclientimplementation as an example)
First, let's recognize a method, which is defined as follows:
/** * Stores data to cache. * * If data does not already exist for this key on the server, or if the key * Is being<br/> * deleted, the specified value won't be stored.<br/> * The server would automatically delete the value when the expiration time * Has been reached.<br/> * <br/> * If compression is enabled, and the data is longer than the compression * threshold<br/> * The data would be stored in compressed form.<br/> * <br/> * As of the current release, all objects stored would use Java * Serialization. * * @param cmdname * Action to take (set, add, replace, Append,prepend, CAs) * @param key * Key to store cache under * @param value * Object to cache * @param expiry * Expiration * @param hashcode * If not null and then the int hashcode * @return True/false indicating success */ Private Boolean set(String cmdname, String key, Object value, Date expiry, Integer hashcode, Long Casunique, Boo Lean asstring) The function of this method is to send instructions to the server to implement various functions and intercept the code snippet of the assembly instruction: Construct the command String cmd = new StringBuffer (). Append (cmdname). Append (""). Append (key) Append (""). Append (""). Append (""). Append ( Expiry.gettime ()/+). Append (""). ToString (); |
thus, to understand the client these methods in the server segment effect, you need to understand the server section of the various instructions to achieve the effect, this is described later.
Examples of methods
Public boolean Add(String key, Object value) { Return Set("Add", key, value, NULL, NULL, 0L, primitiveasstring); } |
The method caches data that does not
Public boolean Add(String key, Object value, Date expiry) { Return Set("Add", key, value, expiry, NULL, 0L, primitiveasstring); } |
This method specifies the expiration time of the data cache (the expiration time will be automatically deleted)
other ways are similar, such as set, replace, Append,prepend, CAS
Note: Key ,value cannot be null.
This article comes from the "Simple and Easy" blog, so be sure to keep this source http://dengshuangfu.blog.51cto.com/8794650/1615976
memcached Java Client 3.0.2 detailed