Memcached常用操作

*生产环境建议直接用linux

1、命令行直接运行
1.1、可以直接指定参数运行

#最大16M内存,监听11211端口,最大连接数8
memcached.exe -m 16 -p 11211 -c 8

1.2、可以注册为Windows服务,再运行

#注册为服务
memcached.exe -d install
#开启服务
memcached.exe -d start
#关闭服务
memcached.exe -d stop
#卸载服务
memcached.exe -d uninstall

1.3、其他参数

-p <num> TCP port number to listen on (default: 11211)
-U <num> UDP port number to listen on (default: 11211, 0 is off)
-s <file> UNIX socket path to listen on (disables network support)
-a <mask> access mask for UNIX socket, in octal (default: 0700)
-l <ip_addr> interface to listen on (default: INADDR_ANY, all addresses)
-s <file> unix socket path to listen on (disables network support)
-a <mask> access mask for unix socket, in octal (default 0700)
-l <ip_addr> interface to listen on, default is INADDR_ANY
-d start tell memcached to start
-d restart tell running memcached to do a graceful restart
-d stop|shutdown tell running memcached to shutdown
-d install install memcached service
-d uninstall uninstall memcached service
-r maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num> max memory to use for items in megabytes (default: 64 MB)
-M return error on memory exhausted (rather than removing items)
-c <num> max simultaneous connections (default: 1024)
-k lock down all paged memory. Note that there is a limit on how much memory you may lock. Trying to allocate more than that would fail, so be sure you set the limit correctly for the user you started the daemon with (not for -u <username> user; under sh this is done with ‘ulimit -S -l NUM_KB’).
-v verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/reponses)
-vvv extremely verbose (also print internal state transitions)
-h print this help and exit
-i print memcached and libevent license
-P <file> save PID in , only used with -d option
-f <factor> chunk size growth factor (default: 1.25)
-n <bytes> minimum space allocated for key+value+flags (default: 48)
-L Try to use large memory pages (if available). Increasing the memory page size could reduce the number of TLB misses and improve the performance. In order to get large pages from the OS, memcached will allocate the total item-cache in one large chunk.
-D <char> Use <char> as the delimiter between key prefixes and IDs.This is used for per-prefix stats reporting. The default is “:” (colon). If this option is specified, stats collection is turned on automatically; if not, then it may be turned on by sending the “stats detail on” command to the server.
-t <num> number of threads to use (default: 4)
-R Maximum number of requests per event, limits the number of requests process for a given connection to prevent starvation (default: 20)
-C Disable use of CAS
-b Set the backlog queue limit (default: 1024)
-B Binding protocol – one of ascii, binary, or auto (default)
-I Override the size of each slab page. Adjusts max item size (default: 1mb, min: 1k, max: 128m)

2、然后是用telnet进行操作
2.1、语法说明

<command name> <key> <flags> <exptime> <bytes>
<data block>
command name 命令名称,如add,set,get,gets,delete等
key 关键字,即KV结构中的key
flags 整型参数,客户机使用它存储关于键值对的额外信息
exptime 该数据的存活时间(以秒为单位,0 表示永远)
bytes 存储字节数
data block 存储的数据块,即KV结构中的value

2.2、操作示例

telnet localhost 11211
#add 用于新增KV
add  key01 0 0 2
11
STORED
#key存在时,add操作失败
add key01 0 0 2
ab
NOT_STORED
#key存在时,set可以进行覆盖操作
set key01 0 0 2
ab
STORED
#准备测试数据
set key02 0 0 2
12
STORED
add key03 0 0 2
13
STORED
add key04 0 0 2
14
STORED
add key05 0 0 2
15
STORED
#读取数值
get key01
VALUE key01 0 2
ab
END
get key02
VALUE key02 0 2
12
END
#一次读取多个值
get key01 key02
VALUE key01 0 2
ab
VALUE key02 0 2
12
END
#读取值与附加信息
gets key03
VALUE key03 0 2 12
13
END
gets key04
VALUE key04 0 2 13
14
END
#一次读取多个值与附加信息
gets key03 key04
VALUE key03 0 2 12
13
VALUE key04 0 2 13
14
END
#修改键值(没有会进行赋值)
set key01 0 0 4
abcd
STORED
#修改键值(参数匹配才能修改,如下面的例子,cas最后一个参数与gets同为11才能修改成功)
gets key02
VALUE key02 0 2 11
12
cas key02 0 0 2 13
cd
EXISTS
cas key02 0 0 2 11
cd
STORED
#键存在,才可以修改成功
get key03
VALUE key03 0 2
13
END
replace key03 0 0 2
ef
STORED
replace key06 0 0 2
xy
NOT_STORED
#在value后面追加数据
get key01
VALUE key01 0 2
cd
END
append key04 0 0 2
bb
STORED
get key01
VALUE key01 0 4
abbb
END
#在value前面追加数据
prepend key01 0 0 2
aa
STORED
get key01
VALUE key01 0 6
aacdbb
END
#增加或减少value数值,增加最终会溢出,减少到0后不再减少
get key05
VALUE key05 0 2
15
END
incr key05 10
25
get key05
VALUE key05 0 2
25
END
decr key05 20
5
decr key05 10
0
#删除键值对
delete key04
DELETED
get key04
END
#清空全部数据
flush_all
OK

2.3、查看状态

#状态信息
stats
STAT pid 5848
STAT uptime 32576
STAT time 1449759115
STAT version 1.4.4-14-g9c660c0
STAT pointer_size 32
STAT curr_connections 10
STAT total_connections 18
STAT connection_structures 11
STAT cmd_get 41
STAT cmd_set 32
STAT cmd_flush 3
STAT get_hits 22
STAT get_misses 19
STAT delete_misses 0
STAT delete_hits 1
STAT incr_misses 0
STAT incr_hits 1
STAT decr_misses 0
STAT decr_hits 2
STAT cas_misses 0
STAT cas_hits 1
STAT cas_badval 1
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 1883
STAT bytes_written 3276
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 288
STAT curr_items 5
STAT total_items 22
STAT evictions 0
END

状态说明:

pid memcache服务器的进程ID
uptime 服务器已经运行的秒数
time 服务器当前的unix时间戳
version memcache版本
pointer_size 当前操作系统的指针大小(32位系统一般是32bit)
rusage_user 进程的累计用户时间
rusage_system 进程的累计系统时间
curr_items 服务器当前存储的items数量
total_items 从服务器启动以后存储的items总数量
bytes 当前服务器存储items占用的字节数
curr_connections 当前打开着的连接数
total_connections 从服务器启动以后曾经打开过的连接数
connection_structures 服务器分配的连接构造数
cmd_get get命令(获取)总请求次数
cmd_set set命令(保存)总请求次数
get_hits 总命中次数
get_misses 总未命中次数
evictions 为获取空闲内存而删除的items数(分配给memcache的空间用满后需要删除旧的items来得到空间分配给新的items)
bytes_read 总读取字节数(请求字节数)
bytes_written 总发送字节数(结果字节数)
limit_maxbytes 分配给memcache的内存大小(字节)
threads 当前线程数

2.4、退出

#退出
quit
Connection to host lost.

2.5、常用操作一览

命令 描述 s示例
get Reads a value get mykey
set Set a key unconditionally set mykey 0 60 5
add Add a new key add newkey 0 60 5
replace Overwrite existing key replace key 0 60 5
append Append data to existing key append key 0 60 15
prepend Prepend data to existing key prepend key 0 60 15
incr Increments numerical key value by given number incr mykey 2
decr Decrements numerical key value by given number decr mykey 5
delete Deletes an existing key delete mykey
flush_all Invalidate specific items immediately flush_all
flush_all Invalidate all items in n seconds flush_all 900
stats Prints general statistics stats
stats Prints memory statistics stats slabs,stats malloc
stats Print higher level allocation statistics stats items,stats detail,stats sizes
stats Resets statistics stats reset
version Prints server version. version
verbosity Increases log level verbosity
quit Terminate telnet session quit

Leave a Reply

Your email address will not be published. Required fields are marked *

*