memcached 命令

http://blog.csdn.net/spring21st/article/details/6541110

 

http://asdfblog.com/technology/memcached-for-win32-command-summary.html

 

telnet <hostname> <port>

In the following \r\n mean you have to type enter in telnet console

If Memcached server send ERROR or CLIENT_ERROR, something is false with the command, if it’s SERVER_ERROR, server itself is in trouble

Retrieval Commands

Parameters :

<key> : the key of the data stored to retrieve

<key>* mean you can request value of multiple keys separated by whitespace

These commands return :

VALUE <flag> <bytes>\r\n<data> (see the following for explanation of <bytes> and <flag>)

END indicate the end of response

Get

Get value from key

Telnet command : get <key>*\r\n

get key
VALUE key 0 4
data
END

Gets

Get value from key along with a cas token (to use with cas command)

Telnet command : gets <key>*\r\n

get key
VALUE key 0 4
data
END

 

Storage Commands

Parameters :

<key> : the key of the data stored

<flags> : 32-bit unsigned integer that the server store with the data (provided by the user), and return along the data when the item is retrieved

<exptime> : expiration time in seconds, 0 mean no delay, if exptime is superior to 30 day, Memcached will use it as a UNIX timestamps for expiration

<bytes> : number of bytes in the data block

<cas unique> : unique 64-bit value of an existing entry (retrieved with gets command) to use with cas command

[noreply] : optional parameter that inform the server to not send the reply

These commands can return :

STORED to indicate success

NOT_STORED indicate that the data was not stored because condition for “add” or “replace” command wasn’t met, or the item is in a delete queue

EXISTS indicate that the item you are trying to store with “cas” command has been modified since last fetch

NOT_FOUND indicate that the item did not exist or has been deleted

Set

Store key/value pair in Memcached

Telnet command : set <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

set key 0 900 4
data
STORED

Add

Store key/value pair in Memcached, but only if the server doesn’t already hold data for this key

Telnet command : add <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

add key 0 900 2
10
STORED

Replace

Store key/value pair in Memcached, but only if the server already hold data for this key

Telnet command : replace <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

replace key 0 900 2
10
STORED

Append

Add value to an existing key after existing data

Append does not take <flags> or <exptime> parameters but you must provide them !

Telnet command : append <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

append key 0 900 2
10
STORED

Prepend

Add value to an existing key before existing data

Prepend does not take <flags> or <exptime> parameters but you must provide them !

Telnet command : prepend <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

prepend key 0 900 2
10
STORED

Cas

Store data only if no one else has updated since the last fetch, determined with the cas tokem from gets command

Telnet command : cas <key> <flags> <exptime> <bytes> <cas unique> [noreply]\r\n

cas key 0 900 2 <cas unique>
10
STORED

 

Delete Command

Parameters :

<key> : the key to delete

[<time>] : optional time in second before real deletion of item

[noreply] : optional parameter that inform the server to not send the reply

These command can return :

DELETED to indicate success

NOT_FOUND indicate that the item did not exist or has been deleted

Delete

Store key/value pair in Memcached

Telnet command : delete <key> [<time>] [noreply]\r\n

delete key
DELETED

 

Increment/Decrement Commands

Parameters :

<key> : the key to delete

<value> : the amount to increase/decrease the item.

[noreply] : optional parameter that inform the server to not send the reply

These command can return :

NOT_FOUND indicate that the item did not exist or has been deleted

<value> increment and decrement return the result

Increment

Increment value associated with key in Memcached, item must exist, increment command will not create it

The limit of increment is the 64 bit mark

Telnet command : incr <key> <value> [noreply]\r\n

incr key 12
56

Decrement

Decrement value associated with key in Memcached, item must exist, decrement command will not create it

If you try to decrement a value bellow 0, value will stay at 0

Telnet command : decr <key> <value> [noreply]\r\n

decr key 12
44

 

Statistics Commands

Parameters :

<args> : argument to stats command

These command can return various stats that we will explain in the following

Basic stats

Return general-purpose statistics like uptime, version, memory occupation, …

Telnet command : stats\r\n

stats
STAT pid 106796
STAT uptime 5696
STAT time 1274397461
STAT version 1.4.5
STAT pointer_size 32
...
STAT total_items 344512124
STAT evictions 0
END

Items stats

Return items statistics, will display items statistics (count, age, eviction, …) organized by slabs ID

Telnet command : stats items\r\n

stats
STAT items:1:number 1125
STAT items:1:age 426
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
...
STAT items:42:tailrepairs 0
END

Slabs stats

Return slabs statistics, will display slabs statistics (size, memory usage, commands count, …) organized by slabs ID

Telnet command : stats slabs\r\n

stats
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 1125
...
STAT active_slabs 25
STAT total_malloced 10485600
END

 

Misc Commands

Flush_all

Flush the server key/value pair (invalidating them) after a optional [<time>] period

It always return OK

Telnet command : flush_all [<time>] [noreply]\r\n

flush_all 10
OK

Version

Return the Memcached server version

Telnet command : version\r\n

version
VERSION 1.4.5

Verbosity

Change the verbosity ouptut of Memcached server

It always return OK

Telnet command : verbosity <level> [noreply]\r\n

verbosity 2
OK

Quit

Server close connection when receiving this command

Telnet command : quit\r\n

quit
======================================================================================================================
=======================================================================================================================
http://lv78.blog.163.com/blog/static/189702048201112224053973/
2011-02-22 14:40:53|  分类: 个人日记 |  标签: |字号 订阅
1、启动Memcache 常用参数
    memcached 1.4.3 
    -p <num>      设置端口号(默认不设置为: 11211) 
    -U <num>      UDP监听端口 (默认: 11211, 0 时关闭) 
    -l <ip_addr> 绑定地址 (默认:所有都允许,无论内外网或者本机更换IP,有安全隐患,若设置为127.0.0.1就只能本机访问) 
    -d            独立进程运行
    -u <username> 绑定使用指定用于运行进程 <username> 
    -m <num>      允许最大内存用量,单位M (默认: 64 MB) 
    -P <file>     将PID写入文件<file>,这样可以使得后边进行快速进程终止, 需要与 -d 一起使用
    如:
    在linux下:./usr/local/bin/memcached -d -u jb-mc -l 192.168.1.197 -m 2048 -p 12121
    在window下:d:\App_Serv\memcached\memcached.exe -d RunService -l 127.0.0.1 -p 11211 -m 500
    在windows下注册为服务后运行:
    sc.exe create jb-Memcached binpath= "d:\App_Serv\memcached\memcached.exe -d RunService -p 11211 -m 500" start= auto
    net start jb-Memcached


2、连接:telnet 127.0.0.1 11211
   不要说不会用这个?

3、写入memcache
   <command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
    a) <command name> 可以是"set", "add", "replace"。
    "set"表示按照相应的<key>存储该数据,没有的时候增加,有的覆盖。
    "add"表示按照相应的<key>添加该数据,但是如果该<key>已经存在则会操作失败。
    "replace"表示按照相应的<key>替换数据,但是如果该<key>不存在则操作失败

    b) <key> 客户端需要保存数据的key。

    c) <flags> 是一个16位的无符号的整数(以十进制的方式表示)。
    该标志将和需要存储的数据一起存储,并在客户端get数据时返回。
    客户可以将此标志用做特殊用途,此标志对服务器来说是不透明的。

    d) <exptime> 过期的时间。
    若为0表示存储的数据永远不过时(但可被服务器算法:LRU 等替换)。
    如果非0(unix时间或者距离此时的秒数),当过期后,服务器可以保证用户得不到该数据(以服务器时间为标准)。

    e) <bytes> 需要存储的字节数(不包含最后的"\r\n"),当用户希望存储空数据时,<bytes>可以为0

    f) 最后客户端需要加上"\r\n"作为"命令头"的结束标志。
    <data block>\r\n

    紧接着"命令头"结束之后就要发送数据块(即希望存储的数据内容),最后加上"\r\n"作为此次通讯的结束。

    结果响应:reply
    当以上数据发送结束之后,服务器将返回一个应答。可能有如下的情况:

    a) "STORED\r\n":表示存储成功
    b) "NOT_STORED\r\n" : 表示存储失败,但是该失败不是由于错误。
    通常这是由于"add"或者"replace"命令本身的要求所引起的,或者该项在删除队列之中。

   如: set key 33 0 4\r\n
        ffff\r\n

4、获取/检查KeyValue
    get <key>*\r\n
    a) <key>* 表示一个或者多个key(以空格分开)
    b) "\r\n" 命令头的结束

    结果响应:reply
    服务器端将返回0个或者多个的数据项。每个数据项都是由一个文本行和一个数据块组成。当所有的数据项都接收完毕将收到"END\r\n"
    每一项的数据结构:
    VALUE <key> <flags> <bytes>\r\n
    <data block>\r\n

    a) <key> 希望得到存储数据的key
    b) <falg> 发送set命令时设置的标志项
    c) <bytes> 发送数据块的长度(不包含"\r\n")
    d) "\r\n" 文本行的结束标志
    e) <data block> 希望接收的数据项。
    f) "\r\n" 接收一个数据项的结束标志。

    如果有些key出现在get命令行中但是没有返回相应的数据,这意味着服务器中不存在这些项,这些项过时了,或者被删除了
    如:get aa
    VALUE aa 33 4
    ffff
    END

5、删除KeyValue:
    delete <key> <time>\r\n

    a) <key> 需要被删除数据的key
    b) <time> 客户端希望服务器将该数据删除的时间(unix时间或者从现在开始的秒数)
    c) "\r\n" 命令头的结束

6、检查Memcache服务器状态:
   stats\r\n
   在这里可以看到memcache的获取次数,当前连接数,写入次数,已经命中率等;

    pid : 进程id
    uptime :总的运行时间,秒数
    time : 当前时间
    version : 版本号
    ......
    curr_items : 当前缓存中的KeyValue数量
    total_items : 曾经总共经过缓存的KeyValue数量
    bytes : 所有的缓存使用的内存量
    curr_connections 当前连接数
    ....
    cmd_get : 总获取次数
    cmd_set : 总的写入次数
    get_hits : 总的命中次数
    miss_hits : 获取失败次数
    .....
    bytes_read : 总共读取的流量字节数
    bytes_written : 总的写入流量字节
    limit_maxbytes : 最大允许使用的内存量,字节

7、高级缓存细节查看方法:
   stats reset
   清空统计数据

    stats malloc
    显示内存分配数据

    stats cachedump slab_id limit_num
    显示某个slab中的前limit_num个key列表,显示格式如下
    ITEM key_name [ value_length b; expire_time|access_time s]
    其中,memcached 1.2.2及以前版本显示的是 访问时间(timestamp)
    1.2.4以上版本,包括1.2.4显示 过期时间(timestamp)
    如果是永不过期的key,expire_time会显示为服务器启动的时间

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

    stats slabs
    显示各个slab的信息,包括chunk的大小、数目、使用情况等

    stats items
    显示各个slab中item的数目和最老item的年龄(最后一次访问距离现在的秒数)

    stats detail [on|off|dump]
    设置或者显示详细操作记录

    参数为on,打开详细操作记录
    参数为off,关闭详细操作记录
    参数为dump,显示详细操作记录(每一个键值get、set、hit、del的次数)

8、清空所有键值 
   flush_all
   注:flush并不会将items删除,只是将所有的items标记为expired,因此这时memcache依旧占用所有内存。

8、退出
   quit
 
 

posted on 2011-08-25 11:44  CodingForever  阅读(1792)  评论(0编辑  收藏  举报

导航