Java memcached release [0]-memcachedclient

Source: Internet
Author: User

1. Set & add & replace
A. If sanitizekeys is true, the key is converted to a UTF-8-encoded string. In case of an exception, if errorhandler is not null, it is called back
Handleerroronset function, return false, value is null exit.
B. Find sockiopool. sockio to obtain a sockio based on key & hashcode. If sockio is null and exits, callback handleerroronset
Function, returns false. If the input expiry is null, it is set to 0, indicating that this operation will never expire. If not, it will be specified to the second.
Click to clear the key-value from the map instead of the back step.
C. Value to byte [] conversion: If nativehandler can process value, and asstring = true indicates that value can be used
Tostring () indicates that its byte [] is the byte [] encoded by the UTF-8 of value. tostring. If tostring is not used
And flag mark nativehandler encoding; otherwise, the Java serialization mechanism is used to implement the byte [] Step, and flag mark serialization
. If an error occurs in the preceding process, the handleerroronset function of errorhandler is called back and the sockio obtained by B is returned. The operation ends. Next
To use gzip to compress the bytes that need to be compressed. If an error occurs, only the handleerroronset is called back and the original byte [] is used.
D. construct the memcached command "% S % d \ r \ N $ value. byte [] $ \ r \ n "is followed by the command name (set | add | replace)-Key
-Flag-expiry-byte []. length, fill in $ value. byte [] $ with byte [], and send it out through sockio; then read the response from the server
Use Readline. If the read STR is stored, the operation is successful, then sockio is returned, and true is returned. If it is not_stored
It indicates that the operation failed. If it is another error-level log record in the response header that should not appear, sockio is returned for the last two cases, and
False. If the ioexception caused by network problems occurs in the above network operations, the handleerroronset is called back before the error level.
Log records, and then disable sockio.
-------------------
2. Get & exits
A. If sanitizekeys is true, the key is converted to a UTF-8-encoded string. In case of an exception, if errorhandler is not null, it is called back
Handleerroronget function, return null.
B. Find sockiopool. sockio to obtain a sockio based on key & hashcode. If sockio is null and exits, callback handleerroronget
Function, returns NULL.
C. Construct the memcached command "get $ key $ \ r \ n" and fill $ key $ with the key and send it out through sockio. Then, read the response from the server,
Readline is executed in an endless loop. If the STR to be read is end, the loop is exited. If the STR to be read is started, the STR is parsed using the $1 $2 $3 rule,
$2 is flag, and $3 is value. byte []. length. Use sockio to fill up byte [] with a length of $3, and then clear the \ r \ n. If the flag is
Perform gzip decompression for compression. If a problem occurs during the process, the handleerroronget function is called back and the nestedioexception is thrown.
Objectinputstream is used to read objects in the stream. To solve the problem, contextobjectinputstream is provided to inject
Classloader is used to solve the problem of resource loading. The handleerroronget function is called back when an error occurs in the serialization process.
Primitiveasstring | if asstring is true, the value is converted to string if it is stored in the string format. Otherwise, nativehandler is used.
The handleerroronget function is called back when an error occurs during decoding and the nestedioexception is thrown. Read the expected end after processing the start
. After the server output is processed successfully, sockio is returned and the parsed object is returned. During network operations or
For ioexception caused by nestedioexception, callback handleerroronget before closing sockio.
D. For exits, it is actually a get operation. If there is no key ing, the system will directly read it to the end to get null.
Is null to determine whether it exists.
-------------------
3. Delete
A. If sanitizekeys is true, the key is converted to a UTF-8-encoded string. In case of an exception, if errorhandler is not null, it is called back
Handleerrorondelete function, return false.
B. Find sockiopool. sockio to obtain a sockio based on key & hashcode. If sockio is null and exits, callback
Handleerrorondelete function, return false.
C. Construct the memcached command "delete $ key $ [$ expiry $] \ r \ n" and fill $ key $ with the key. If there is an expiration time, it will be invalid.
D. time filling $ expiry $. It is sent out through sockio. Then, read the response from the server and run Readline. If STR is deleted
The deletion operation is successful, then sockio is returned, and true is returned. Otherwise, sockio is returned, and false is returned. In the above process, if
If ioexception occurs due to network reasons, the handleerrorondelete function is called back and sockio is disabled.
-------------------
4. Counter
There are four counter operations: storecounter, getcounter, addorincr | addordecr, incr | decr.
A. storecounter operations are mainly to put key-counter into the cache through the set operation, without a valid time setting.
B. The getcounter operation is mainly to get the counter corresponding to the key in the string form through the get operation, and then convert it to long. If
If an exception occurs in the get operation, the handleerroronget function is called and-1 is returned.
C. addorincr | the addordecr operation first uses the add operation. If the value already exists, false is returned, and further processing is performed. If the key is not saved
Form a ing. In this way, the atomic operations of memcached can ensure thread security in a distributed or multi-threaded environment. Distributed with zookeeper
Provides the same functions. If the key already exists, use the incrdecr logic:
--------
1. If sanitizekeys is true, the key is converted to a UTF-8-encoded string. In case of an exception, if errorhandler is not null, it is called back
Handleerroronget function, return-1.
2. Find sockiopool. sockio to obtain a sockio based on key & hashcode. If sockio is null and exits, callback handleerroronset
-1 is returned.
3. Construct the memcached command "% S % d \ r \ n" and enter the command name incr | decr, key, and value in sequence. Send it out through sockio; then
Read the response from the server and run Readline. If the read STR matches the mode "\ D +", the operation is successful and the returned server performs
Incr | counter value after decr operation, then return sockio, and return the value converted to long. If the value range of Long is exceeded, callback
Handleerroronget function, return-1; if STR does not conform to the mode "\ D +", return sockio and return-1. In the above process, if
If ioexception occurs, the handleerroronget function is called back, sockio is disabled, and-1 is returned.
--------
D. incr | decr operations mainly target counter operations of existing keys. For the procedure, see the description above. If the key is not
If yes, the STR to be read is not_found to get-1.
-------------------
5. multi
A. If sanitizekeys is true, all string [] keys are converted to a UTF-8-encoded string. If an exception occurs in this case
If errorhandler is not null, The handleerroronget function is called back, or the key is null. If keys has the corresponding hashcode
Obtain a sockio based on key & hashcode. If sockio is null, It is not processed. The operation continues.
Classification to form host-> "get keys [N] keys [m]…" After processing, return all sockio borrowed in this step.
B. The map of commands corresponding to each machine is obtained in the previous step, and a map is instantiated to store the final result.
C. Use nioloader for concurrent get operations
-------
1. selector. open () gets the Selector Used for this concurrent get, iterates the map of the command corresponding to each machine, and obtains
One sockio. If one of the sockio values is null, exit the entire get operation [disable selector before exiting, all
Disable socketchannel and disable all sockio operations.] Create an underlying data structure for each sockio:
List <bytebuffer>, the memcached command sent to this machine, the socketchannel handle for network communication, and set it to non-blocking mode,
Register the op_write event on the selector.
2. Perform the while loop round-robin For the event listening on the selector. There are two exit conditions: The data of all servers is read completely or exceeds
Maxbusy time settings. When the socketchannel can process write, write the memcached command sent to this machine to the selector.
Register the op_read event. When the socketchannel can process the read, it goes to the list <bytebuffer> used to receive the data to get the current number of recipients.
Data bytebuffer, read, this linked list is each instantiated 8 KB space, each time the data is read, will check the last few bytes read is
It is not "End \ r \ n". If the received data is complete, the while loop checks the number of data reads completed at the exit server ++ to cancel the selector
Listen.
3. There are three scenarios for Step 2 to end: When an exception occurs during network operations, the handleerroronget function is called back and the entire multi
Operation [disable selector before exiting, disable all socketchannels, and disable all sockio]; successful completion of all commands and
When receiving data, the selector is disabled, the blocking mode is set for all socketchannels, and all sockio return operations are performed before data sorting.
The selector is disabled because of timeout. All socketchannels and all sockio close operations are not completed.
For the completion of all socketchannel settings blocking mode and all sockio return operations, and then complete the data sorting.
4. data sorting steps: Use bytebufarrayinputstream to encapsulate the list <bytebuffer> used to receive data, and execute Readline cyclically.
If it starts with value, the STR read is separated by the mode "$0 $1 $2 $3". $0 is value, $1 is key, and $2 is flag, $3 is
Value. length, read and parse the value according to the information, and then put the key-value into the map used to store the final result; If STR is
End is used to exit the loop.
-------
D. Multi operations can be divided into two types: one is to return a corresponding object []; the other is to return only the map of the final result of the key that follows the normal flow.
-------------------
6. flushall
A. You can know which servers or all servers are cleared at a specified point, iterate the servers to be cleared, and obtain sockio from the connection pool. If
If the value is not null, The memcached command "flush_all \ r \ n" is constructed and sent. Then, the sockio is used for Readline operations.
If yes, the task is cleared successfully. If no network exception is sent in the operation, return sockio; otherwise, disable sockio.
-------------------
7. This is the action of the memcachedclient operation layer, mainly set & add & replace, get & exits, delete, counter,
Multi and flushall operations, where counter uses step-by-step operations to ensure data security. This layer mainly uses the underlying sockiopool
Io pooling management is mainly used to borrow a sockio based on key & hashcode or host, and return sockio after use,
Alternatively, a network exception occurs during the operation or sockio is disabled due to timeout.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.