memcached 操作

参见文章

1.访问memcached

telnet 127.0.0.1 11211

2.显示状态

stats

pid = process id
uptime = number of seconds since the process was started
time = current time
version = memcached version
rusage_user = seconds the cpu has devoted to the process as the user
rusage_system = seconds the cpu has devoted to the process as the system
curr_items = total number of items currently in memcache
total_items = total number of items that have passed through the cache
bytes = total number of bytes currently in use by curr_items
curr_connections = total number of open connections to memcached
connection_structures = ???
cmd_get = total GET commands issued to the server
cmd_set = total SET commands issued to the server
get_hits = total number of times a GET command was able to retrieve and
return data
get_misses = total number of times a GET command was unable to retrieve and
return data
bytes_read = total number of bytes input into the server
bytes_written = total number of bytes written by the server
limit_maxbytes = total storage bytes available to the server.

    limit_maxbytes、bytes

    memcached在存储的时候是可以设置失效时间的,但如果存储已经满了,那旧数据即使没有到过期时间,也会被移除。所以需要观察memcached存储是否已经满了,同时这对扩容也是有意义的参考。limit_maxbytes即总的存储大小,而bytes就是已经使用的大小,从这两个数据就可以看出在memcached启动时,我们为它分配的内存是否足够使用。

    cmd_get、cmd_set

    memcached启动后,我们对它一共做了多少次读取操作呢?从这两个参数可以观察出来。

    get_hits、get_misses

    使用memcached后,我们需要评估我们使用的策略是否合理。不能够使用中间缓存后,后端的数据库还是有较大的访问量,这样的话中间缓存就变得没有意义了。get_hits表示命中了多少次读取,即来memcached取到了多少有效数据;get_misses表示没有命中的次数,即此次来取数据的时候,memcached并没有你所查询的数据。如果没有清零统计数据的话,cmd_get = get_hits + get_misses。

3. 清空统计数据

stats reset

4.显示某个slab中的前limit_num个key列表

stats cachedump 1 2


5. 显示slabs信息

stats slabs

6.命令说明

<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
当以上数据发送结束之后,服务器将返回一个应答。


7.添加一个新的条目到memcached或是用新的数据替换掉已存在的条目

set key 0 0 value长度
value

set test1 0 0 10
testing001


8.仅当key不存在的情况下存储数据。如果一个key已经存在,将得到NOT_STORED的响应

add key 0 0 value长度
value

add test1 0 0 10
testing002

9.仅当key已经存在的情况下存储数据。如果一个key不存在,将得到NOT_STORED的响应

replace key 0 0 value长度
value

replace test1 0 0 10
testing002

10.从memcached中返回数据。从缓存中返回数据时,将在第一行得到key的名字,flag的值和返回的value的长度。真正的数据在第二行,最后返回END

get key

get testing1

11.启动Memcache 常用参数

-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:\memcached\memcached.exe -d RunService -l 127.0.0.1 -p 11211 -m 500
在windows下注册为服务后运行:
sc.exe create my-Memcached binpath="d:\memcached\memcached.exe -d RunService -p 11211 -m 500" start=auto
net start my-Memcached

12.显示所有条目

stats items

13.删除条目

delete <key> <time>

a) <key> 需要被删除数据的key
b) <time> 客户端希望服务器将该数据删除的时间(unix时间或者从现在开始的秒数)

14. 清空所有键值

flush_all


15.退出

quit






猜你喜欢

转载自blog.csdn.net/johnnywww/article/details/8484782