You often need to perform some operations on memcache data when locating problems and testing. However, there is no self-built client, so you can only perform operations through telnet.
Connect to memcache through telnet.
Copy codeThe Code is as follows:
Telnet fig 11211
Memcache write operations
Telnet write memcache Command Format
Copy codeThe Code is as follows:
<Command name> <key> <flags> <exptime> <bytes> \ r \ n <data block> \ r \ n
Parameters:
1. <command name> can be set, add, or replace ".
Set: If <key> does not exist, it is added. If <key> exists, it is overwritten.
Add: If <key> does not exist, the operation fails if <key> exists.
Replace: If <key> does not exist, data cannot be added. If <key> exists, data is replaced.
2. <key> the key to save the data
3. <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 sign for special purposes, which is transparent to the server.
4. <exptime> expiration time.
0 indicates that the stored data will never expire (but can be replaced by server algorithms such as LRU ).
Non-zero (unix time). After expiration, the server can ensure that the user does not get the data (based on the server time ).
5. <bytes> the number of bytes to be stored (excluding the last "\ r \ n"). If you want to store empty data, the value can be 0.
6. Add "\ r \ n" to the client as the end mark of the "Command Header.
7. <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.
Telnet response command
After the above data is sent, the server returns a response. The following situations may occur:
1. STORED \ r \ n: indicates that the storage is successful.
2. 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, or the item is in the delete queue.
Example
Copy codeThe Code is as follows:
Set key 32 0 10 \ r \ n
Helloworld \ r \ n
STORED \ r \ n
Get Key & Value
Get Command Format
Copy codeThe Code is as follows:
Get <key> * \ r \ n
Parameters:
1. <key> *: indicates one or more keys (separated by spaces)
2. \ r \ n: Command end
Response Results
The server returns zero or more data items. Each data item is composed of a text row and a data block. When all data items are received, the data structure of END \ r \ n is received:
Copy codeThe Code is as follows:
VALUE <key> <flags> <bytes> \ r \ n
<Data block> \ r \ n
Parameters:
1. <key> key of the stored data
2. <falg> flag set when the set command is sent
3. <bytes> length of the data block to be sent (excluding "\ r \ n ")
4. \ r \ n end mark of the text line
5. <data block> the data item to be received.
6. \ r \ n receives the end mark of a data item.
PS: if some keys appear in the get command line but no corresponding data is returned, this means that there is no change in the server, which may be timeout or deleted.
Example
Copy codeThe Code is as follows:
Get key
VALUE key 32 10
Helloworld
END
Delete KeyValue:
Copy codeThe Code is as follows: delete <key> <time> \ r \ n
Parameters:
1. <key> key of the data to be deleted
2. <time> time when the client wants the server to delete the data (unix time or number of seconds from now on)
3. End of the \ r \ n Command Header
Check the Memcache Server Status
Copy codeThe Code is as follows: stats \ r \ n
Here we can see the number of times memcache is obtained, the current number of connections, the number of writes, the hit rate, and so on;
Copy codeThe Code is as follows:
Pid: process id
Uptime: total running time, in seconds
Time: Current time
Version: version Number
......
Curr_items: Number of keyvalues in the current Cache
Total_items: Total number of cached keyvalues
Bytes: The amount of memory used by all caches
Current number of curr_connections connections
....
Pai_get: Total number of times of obtaining
Performance_set: Total number of writes
Get_hits: Total number of hits
Miss_hits: number of failed requests
.....
Bytes_read: Total number of read traffic bytes
Bytes_written: Total write traffic bytes
Limit_maxbytes: maximum memory usage, in bytes
Clear statistics:
Copy codeThe Code is as follows:
Stats reset
Clear all key values
Copy codeThe Code is as follows:
Flush_all
Note: flush does not delete items, but marks all items as expired. Therefore, memcache still occupies all memory.
Exit
Copy codeThe Code is as follows:
Quit \ r \ n
Other commands
1. display information about each slab, including the chunk size, quantity, and usage.
Copy codeThe Code is as follows: stats slabs
2. display the number of items in each slab and the age of the oldest item (the number of seconds from the last visit)
Copy codeThe Code is as follows: stats items
3. Set or display detailed operation records
Copy codeThe Code is as follows: stats detail [on | off | dump]
The parameter is on.
The parameter is off. Disable detailed operation records.
The parameter is dump, and detailed operation records are displayed (the number of times each key value is get, set, hit, and del)
4. display the list of previous limit_num keys in a slab
Copy codeThe Code is as follows: stats cachedump slab_id limit_num
The display format is as follows:
Copy codeThe Code is as follows: ITEM <key_name> [<value_length> B; <expire_time | access_time> s]
<Key_name>: key name
<Value_length>: Value length (in bytes)
<Expire_time | access_time>: memcached 1.2.2 and earlier versions display the access time (timestamp)