Memcached command
- Standard protocol
- Storage commands
- SET command
- Add command
- Replace command
- APPEND Command
- Prepend command
- CAS command
- READ command
- DELETE command
- Incr/decr command
- View memcached Usage Status
- Stats command
- Stats items command
- Stats slabs command
- Stats sizes command
- Flush_all command
All protocols are in Protocol documentation.
Standard protocol
All memcached standard protocols are included in the execution of commands on items. An item contains:
- One key
- A 32-bit flag value
- Expiration time in seconds
- A 64-bit CAS value, which is unique
- Data
CAS is optional. You can use
-C
"Disable CAS.
No reply
Most ASCII commands allow "noreply ". We recommend that you do not use "noreply" in the ASCII protocol, because the request error will not be reported. "Noreply" is designed to avoid waiting for the returned package after interactive commands (such as set and add) are executed.
The binary Protocol defines "noreply" as declaration. If your client supports or uses the binary protocol, you will use it.
Storage commands
First, the client sends a command line to the server in the following format:
<Command name> <key> <flags> <exptime> <bytes> \ r \ n
A) <command name> can be set, add, or replace ".
"Set" indicates that the data is stored according to the corresponding <key>.
"Add" means to add the data according to the corresponding <key>, but if the <key> already exists, the Operation will fail.
"Replace" indicates that data is replaced according to the corresponding <key>, but the operation fails if the <key> does not exist.
B) <key> key of the data to be saved on the client.
C) <flags> is a 16-bit unsigned integer (expressed in decimal format ). This flag is stored together with the data to be stored and returned when the client get data. The customer can use this flag for special purposes, which is not transparent to the server.
D) <exptime> expiration time. If the value is 0, the stored data will never expire (however, the data may be replaced by other items. This is because the server uses the LRU (not used for the longest time) algorithm ). If it is not 0 (UNIX time or the number of seconds from this time point), after expiration, the server can ensure that the user does not get the data (based on the server time ).
E) <bytes> the number of bytes to be stored (excluding the last "\ r \ n"). If you want to store empty data, the value <bytes> can be 0.
F) Add "\ r \ n" to the client as the end mark of the "Command Header.
<Data block> \ r \ n
After the "Command Header" ends, you need to send a data block (that is, the data content you want to store), and add "\ r \ n" to end the communication.
Reply
After the above data is sent, the server returns a response. The following situations may occur:
A) "stored \ r \ n"
Indicates that the storage is successful.
B) "not_stored \ r \ n"
Indicates that the storage fails, but this failure is not due to an error. This is usually caused by the requirements of the "add" or "replace" command itself, or the item is in the delete Queue (see the DELETE command ).
Set
Set is a command to save data. Existing data will be overwritten, and new data will be at the top of LRU
Add
The data is saved only when the data does not exist. If it is a newly added item, place it directly on the top of LRU; if the item already exists, causing the Add failure, remove the item from the LRU linked list and put it on the top of LRU.
Replace
Replace existing data. This operation is rarely used.
Append
Add an item to an existing item. This operation does not allow you to add the original item limit, which is useful for managing linked lists.
Prepend
Similar to the APPEND Command, this command adds new data before existing data.
CAS
Check and store (
Check and set) or compare and update (compare and swap ). If there are no updates since the last read, it is useful to store data and process the update competition.
READ command
Format of the retrieved data:
Get <key> * \ r \ n
A) <key> * indicates one or more keys (separated by spaces)
B) End of the "\ r \ n" Command Header
Reply
The server returns zero or more data items. Each data item is composed of a text row and a data block. After receiving all data items, you will receive "End \ r \ n"
Data structure of each item:
Value <key> <flags> <bytes> \ r \ n
<Data block> \ r \ n
A) <key> key of the stored data
B) <falg> flag set when the set command is sent
C) <bytes> length of the sent data block (excluding "\ r \ n ")
D) End mark of "\ r \ n" text line
E) <data block> the data items to be received.
F) "\ r \ n" receives the end mark of a data item.
If some keys appear in the GET command line but no corresponding data is returned, this means that these items do not exist in the server. These items are outdated or deleted.
Get
Read the command, search for data with one or more keys, and return the data found.
Gets
Using the GET command of CAS, the returned item carries a CAS identifier (a unique 64-digit ). Use the CAS command to return data. If the CAS value of the obtained item is changed, the data will not be saved.
DELETE command
If an item exists, delete it from the cache,
DELETE command format:
Delete <key> <time> \ r \ n
A) <key> key of the data to be deleted
B) <time> time when the client wants the server to delete the data (UNIX time or number of seconds from now on)
C) End of the "\ r \ n" Command Header
Reply
A) "deleted \ r \ n" deleted successfully
B) "not_found \ r \ n", the key to be deleted does not exist.
Incr/decr
Increment and decrement. If the item is stored in 64 Integer type, you can use the incr and decr commands to modify the number.
If the data does not exist, a failure is returned.
Command Format:
Incr <key> <value> \ r \ n
Or
Decr <key> <value> \ r \ n
A) <key> key of the data item
B) <value> the value of the data that you want to increase/decrease. The value is a 32-bit unsigned integer variable.
C) "\ r \ n" Command Line End mark
Reply
A) "not_found \ r \ n" does not find the item to be operated.
B) "<value> \ r \ n" <value> the latest remaining time of the data item validity period.
Note:
A) if the validity period of a data item is set to 0, the decr command cannot reduce the data.
B) If you want to perform the incr key-1 operation, the result is the same as what you want. However, the result of executing decr-1 will surprise you, because the result of decr-1 is zero regardless of the data of the key. the reason is: during the execution of these two commands, it is like-1 as an unsigned integer.
C) when running the decr command, the data length does not decrease, but fills in spaces after the returned data. However, when the incr command is executed out of bounds, the number of data digits is automatically increased.
View memcached Usage Status
You can use these commands to view the Usage Status of the memcached server.
Stats
Run the following command to view the memcached status:
Stat PID 22459 process ID Stat uptime 1027046 server running seconds Stat time 1273043062 server current UNIX Timestamp Stat version 1.4.4 server version Stat pointer_size 64 OS font size (this server is 64-bit) Stat rusage_user 0.040000 cumulative process user time Stat rusage_system 0.260000 cumulative system time of processes Stat curr_connections 10 number of currently opened connections Stat total_connections 82 total number of connections opened Stat connection_structures 13 Number of connection structures allocated by the server Stat cmd_get 54 total number of get commands executed Stat cmd_set 34 Total number of set commands executed Stat cmd_flush 3 points to the total number of flush_all commands Stat get_hits 9 get hits Stat get_misses 45 get Miss times Stat delete_misses 5 delete miss times Stat delete_hits 1 Delete hits Stat incr_misses 0 incr miss count Stat incr_hits 0 incr hits Stat decr_misses 0 decr miss count Stat decr_hits 0 decr hits Stat cas_misses 0 CAS miss count Stat cas_hits 0 CAS hits Stat cas_badval 0 used for wiping Stat auth_cmds 0 Stat auth_errors 0 Stat bytes_read 15785 total number of bytes read Stat bytes_written 15222 total number of written bytes Stat limit_maxbytes 1048576 memory allocated (bytes) Stat accepting_conns 1 currently accepted connections Stat listen_disabled_num 0 Stat threads 4 threads Stat conn_yields 0 Stat bytes 0 stores item bytes Stat curr_items 0 item count Stat total_items 34 Total items Stat evictions 0 indicates the total number of items deleted from the retrieved space.
|
Stats items
Output the item information of each slab. S
Stats Slabs
Output more detailed item information in Slab
Stats sizes
Output the size and number of all items
Stats cachedump <slab_id> <limit_num>
Output the item information in the same <slab_id> according to <slab_id>. <Limit_num> indicates the number of output items. If <limit_num> is 0, all items are output. Flush_all
Invalidates all items in the memory. When a parameter is added, all items are invalid after n seconds. This operation will return immediately and will not suspend the server. This operation does not actually release the memory space, but indicates that all items are invalid.
From: http://blog.chinaunix.net/u1/45336/showart_2227790.html