memcache common operations

 

 

 

Command Description Example
get Read key get mykey
set Set up a new key set mykey 0 60 5
add Add key add newkey 0 60 5
replace Replace an existing value replace key 0 60 5
append Value added at the end append key 0 60 15
prepend Adding value head prepend key 0 60 15
incr Incremental value incr mykey 2
decr Decreasing value decr mykey 5
delete Delete key delete mykey
flush_all Clear all data flush_all
Clear data in the n seconds flush_all 900
stats Print all status information stats
Print memory information stats slabs
Print memory information stats malloc
Advanced Information stats items
  stats detail
  stats sizes
Reset state stats reset
version Print Server (memcached) version version
verbosity Log Level verbosity
quit Exit telnet console quit

 

1.telnet request command format

<Command name> <Key> <the flags> <exptime> <bytes> \ R & lt \ n-<Data Block> \ R & lt \ n-
A) <Command name> can be "set", "add", "replace".
Or "set" stores the data in the corresponding <key>, when there is no increase, some covering.
"Add" indicates the data is added in accordance with the corresponding <key>, but if the <key> will already failed.
"Replace" expressed in terms of the corresponding <key> replacement data, but if the <key> is not present then the operation fails

b) <key> key client needs to store data.

c) <flags> is an integer 16-bit unsigned (in decimal manner).
This flag will be stored with the data needs to be stored, and returned when the client get data.
Customers can use this flag for special purposes, this flag is opaque to the server.

d) <exptime> expiration time.
If the stored data is always 0, but when expressed (but may be a server algorithm: LRU replacement, etc.).
If non-0 (unix time or distance at this time is the number of seconds), after the expiration, the server can not guarantee that the user data (server time as the standard).

e) <bytes> number of bytes to be stored (not included in the final "\ r \ n"), when the user wishes to store null data, <bytes> may be 0

f) Finally, the client needs to add "\ r \ n" as "command head" end mark.
<data block> \ r \ n

Followed by "first order" will transmit a data block (i.e., like the data stored content) after the end of the last add "\ r \ n" as the end of this communication.

2. telnet command response

Results Response: reply
when the above data after the transmission end, the server returns a response. You may have the following situation:

a) "STORED \ r \ n ": indicates that the store successfully
b) "NOT_STORED \ r \ n ": indicates the store failed, but the failure is not due to an error.
Usually this is due to "add" or "replace" command requires itself caused, or delete the item in the queue.

Ru: 4 And set key 33 And of 0 \ r is \ n
ffff \ r is \ n

3. Get / Check the KeyValue
GET <Key> * \ R & lt \ n-
A) <Key> * represents one or more key (separated by spaces)
end b) "\ r \ n" command header

Results Response: reply
server returns zero or more data items of. Each data item is one of a line of text and data blocks. When all items have been received will receive the "END \ r \ n"
each of the data structures:
of VALUE <Key> <the flags> <bytes> \ R & lt \ n-
<Data Block> \ R & lt \ n-

a) <key> want to store data Key
B) <falg> provided when sending the set command flag item
c) <bytes> length of the transmission data blocks (excluding "\ R & lt \ n-")
D) "\ R & lt \ n "marks the end of the text line
e) <data block> expects to receive the data item.
f) "\ r \ n" end flag receiving a data item.

If you get some key appears in the command line, but did not return the appropriate data, which means that these items do not exist in the server, these items out of date, or is deleted
, such as: get AA
of VALUE AA 33 4
ffff
END

4.删除KeyValue:
delete <key> <time>\r\n

a) <key> data to be deleted Key
B) <Time> client wants the server to delete the data time (unix time or the number of seconds from now)
C) "\ R & lt \ n-" command to end the head

5. Check the server status Memcache:
stats \ R & lt \ n-
can be seen here memcache acquired number of current connections, the number of writing, and the like has been hit rate;

pid: process the above mentioned id
Uptime: Total running time, the number of seconds
time: Current time
version: The version number
......
curr_items: current cache KeyValue number
total_items: once after the cache KeyValue total number of
bytes: All cache memory used amount
curr_connections the current number of connections
....
cmd_get: get the total number of
cmd_set: the total number of writing
get_hits: the total number of hits
miss_hits: get the number of failures
... ..
bytes_read: total number of bytes read flow
bytes_written: total write traffic bytes
limit_maxbytes: maximum allowable amount of memory bytes

6. Advanced caching methods View details:
stats the RESET
Clear statistics

stats malloc
display memory allocation data

stats cachedump slab_id limit_num
display front limit_num a key list in a slab, and is displayed as
ITEM key_name [value_length b; expire_time | access_time s]
wherein, memcached 1.2.2 and previous versions shows the access time (timestamp)
1.2.4 above versions, including 1.2.4 display an expiration time (timestamp)
if it is never expires key, expire_time displays the time for the server to start

stats cachedump 7 2
ITEM copy_test1 [250 b; 1207795754 s]
ITEM copy_test [248 b; 1207793649 s]

stats slabs
display information of each slab, including the chunk size, number, and other usage

stats items
display the number and age of the oldest item in the item of each slab (last accessed in seconds from now)

stats detail [on | off | dump ]
setting or display detailed recording operation

Parameter to on, open the detailed operation of the recording
parameters is off, closing the detailed operation of the recording
parameters dump, the display (the number of times each key get, set, hit, del) of the detailed operation of the recording

7. Empty all keys
flush_all
Note: flush items will not be deleted, but all of the items marked as expired, then memcache therefore still occupied all the memory.

8, quit
quit \ r \ n

 

 

 

memcache telnet maintenance Summary

           As memcache cache outside an excellent process, it is often used in highly concurrent systems architecture. Here to talk about how the main tools via telnet, see the memcache maintain health and manage their key. Assume memcache installation directory: / usr / local / memcached

          
1, start memcache

[root@localhost ~]# /usr/local/memcached/bin/memcached -d -m 512  -u root -l 192.168.119.70 -p 12000 -c 512 -P /usr/local/memcached/memcached.pid

Detailed Startup Parameters
 -d: in daemon mode starts. If this parameter is not specified, when the end of the press command ctrl + c, memcache automatically closed
 -m: The maximum memory allocated to the number of units is used memcache m, default 64M
 -u: user specified run memcache
 -l: Specifies the listening ip address
 -p: Specifies the listening port number tcp, udp port can be specified by -u default is 11211.
 -c: the maximum number of concurrent connections
 -P: error process id file
 after starting memcache, memcache we can connect via telnet, to its simple operation and management.

2, telnet connection memcache

[root@localhost ~]# telnet 192.168.119.70 12000 
Trying 192.168.119.70...
Connected to 192.168.119.70 (192.168.119.70).
Escape character is '^]'. 

 After a successful connection, you can manage the operation of the memcache, commonly used commands are:

Ⅰ, add, modify

        命令格式:<command> <key> <flags> <exptime> <bytes>\r\n<data block>\r\n

       <command>:add, set或 replace

       <Key>: cache name

       <Flag>: 16 bit unsigned integer, and key data to be stored in the storage together, and when the program get cache returns.

      <Exptime>: Over time, 0 means never expires, if non-zero, this represents a unix time or from the number of seconds

       <Bytes>: the number of bytes of data stored

        \ R \ n: represents a carriage return linefeed

        Command results:

                STORED: for success

                NOT_STORED: indicates failure 

a), add cache

add id 1 0 4
1234
STORED

         If the key already exists, it will be added.

b), change the cache

replace id 1 0 4
3456
STORED

     When there is key, success; when there is no failure.

c), the buffer provided

set id 1 0 4
2345
STORED

    When the key does not exist, add [add]; already exists when, alternatively replace [].

 

Ⅱ, read

       Format: get <key> + \ r \ n

       <Key> +: represents one or more key, a plurality of key, separated by spaces

a), read a single key buffer

get id
VALUE id 1 4
1234
END

 

 b), a plurality of read cache key of

get id name
VALUE id 1 4
3456
VALUE name 1 3
jim
END

 

Ⅲ, delete

Syntax: delete <key> \ r \ n

<Key>: to delete key

Delete id

delete id 
DELETED

 

Ⅳ, clear all cache

Format: flush_all

flush_all
OK

 

Ⅴ, view the cache server status

Command: stats

stats 
STAT pid 2711 // process the above mentioned id 
STAT Uptime 2453 // total running time, Units Description 
STAT time 1344856333 // current time 
STAT version 1.4.0 // version 
STAT pointer_size 32 // server pointer median, the general 32-bit operating system 32 is a 
cumulative user time STAT rusage_user 0.002999 // process 
STAT rusage_system cumulative process of system events 1.277805 // 
STAT curr_connections 1 // current number of connections 
after STAT total_connections 11 // server startup, the total number of connections 
STAT connection_structures 11 // connection structure the number of 
STAT cmd_get 17 // get the total number of 
STAT cmd_set 1 // total number of writing 
STAT cmd_flush 1 // total number of empty 
STAT get_hits 1 // total number of hits 
STAT get_misses 7 // do not get hit count 
STAT delete_misses // delete not hit count 
STAT delete_hits 4 // delete the hit count  
STAT incr_misses // increment operation did not hit count
STAT incr_hits // incrementing the number of hits 
STAT decr_misses // decrement did not hit the number 
STAT decr_hits // decrement the number of hits 
STAT cas_misses // cas no set number of hits 
STAT cas_hits // cas number of hits 
STAT cas_badval // cas operation found key, but the version expired, there is no set successfully 
STAT bytes_read 455 // get a total amount of data 
STAT bytes_written 1175 // total amount of data written 
STAT limit_maxbytes 1048576 // maximum allowed memory, in bytes 
STAT accepting_conns 1     
STAT listen_disabled_num 0 
STAT 5 Threads // current number of threads 
STAT conn_yields 0 
STAT // 56 bytes buffer space has been used 
STAT curr_items 1 // currently cached number keyvalue 
number keyvalue STAT total_items 7 // total cache, including expired deleted 
STAT evictions // by deleting keyvalue, release the number of memory 
END

Ⅵ, Print version

Command: version

version
VERSION 1.4.0

Ⅶ, print information memory

Command: stats slabs

stats slabs
STAT 1:chunk_size 80
STAT 1:chunks_per_page 13107
STAT 1:total_pages 1
STAT 1:total_chunks 13107
STAT 1:used_chunks 1
STAT 1:free_chunks 1
STAT 1:free_chunks_end 13105
STAT 1:get_hits 10
STAT 1:cmd_set 10
STAT 1:delete_hits 4
STAT 1:incr_hits 0
STAT 1:decr_hits 0
STAT 1:cas_hits 0
STAT 1:cas_badval 0
STAT active_slabs 1
STAT total_malloced 1048560
END

 

3, exit telnet 

quit

 

 

 

 

 

memcached telnet related operations

 

elnet localhost 11211
//保存
set good 32 0 10
helloworld
STORED

//取回
gets good
VALUE good 32 10 10
helloworld
END

//替换
replace good 32 0 10
worldhello
STORED
get good
VALUE good 32 10
worldhello
END

// add tail
the append. 5 0 Good 32
After
STORED
GET Good
of VALUE 32 15 Good
worldhelloafter
the END

//头部添加
prepend good 32 0 6
before
STORED
get good
VALUE good 32 21
beforeworldhelloafter
END

// Delete
the Delete Good
DELETED
GET Good
END


delete good
NOT_FOUND


Cas good 32 0 10 hel
helloworld
Exists

gets good
VALUE good 32 10 10
helloworld
END


cas bad 32 0 10 good
worldhello
NOT_FOUND

 

 

Reference article:

memcache operation command at the telnet 

memcache telnet maintenance summary 

Use memcache telnet command

memcached comprehensive analysis -PDF summary article    * worth reading many times

About memcached features some of the things of the Memcached

Memcached Knew children

View, analyze, use memcached state

 

Guess you like

Origin www.cnblogs.com/52py/p/11236166.html