前面我们简单的介绍了一下Memcached,这里我们再来详细说说Memcached的一些命令。
Memcached的命令总的来说分为三类:存储数据命令、读取数据命令和统计分析命令。
存储数据命令,主要有:set,add,replace,append,prepend,cas;前面已经演示了set的用法,典型的格式如下所示:
command key flag expiration_time bytes value
- key 数据的键
- flag key/value之外的额外信息,也有人把它称之为数据类型,必须为数字
- expiration_time 过期时间,单位为秒,0表示永不过期
- bytes 存储数据所占的字节数,必须完全匹配,不然会存储失败
- value 表示key对应的数据值,在回车后第二行输入
set在存储数据时,如果该key已经存在,会更新对应的value值,成功会返回STORED。
set key 0 0 4 liyd STORED
add命令表示在存储时,哪果已经存在会操作失败,成功返回STORED,失败返回NOT_STORED。
add key 0 0 4 liyd NOT_STORED add key2 0 0 4 liyd STORED
replace命令表示替换的意思,会更新value的值,和add刚好相反。如果key/value不存在,replace会操作失败,成功返回STORED,失败返回NOT_STORED;
注意replace可以改变原先存储数据的长度。
replace key 0 0 6 selfly STORED replace key3 0 0 6 selfly NOT_STORED
append命令表示在指定key对应的value值后追加数据,key必须已经存在,否则会操作失败,成功返回STORED,失败返回NOT_STORED;
注意指定的数据长度是当前添加的长度,非总长度。
set key 0 0 4 liyd STORED append key 0 0 6 selfly STORED get key VALUE key 0 10 liydselfly END append key4 0 0 4 liyd NOT_STORED
prepend命令表示在指定key对应的value值的前面追加数据,和append相反,同样key必须已存在,否则会操作失败,成功返回STORED,失败返回NOT_STORED;
set key 0 0 4 liyd STORED prepend key 0 0 6 selfly STORED get key VALUE key 0 10 selflyliyd END prepend key4 0 0 4 liyd NOT_STORED
cas (check and set):先比较后更新,在存储数据时会要求带上版本号,memcached会比对这个版本号与当前存储数据的是否相等,相等才会更新数据,否则操作失败,成功返回STORED,失败返回EXISTS。
通常在并发操作时使用,版本号可以在读取数据时使用gets命令获取。
gets key VALUE key 0 10 12 selflyliyd END cas key 0 0 4 11 //版本号!=12,更新失败 liyd EXISTS cas key 0 0 4 12 //更新成功 liyd STORED gets key VALUE key 0 4 13 //版本号变为13 liyd END
数据读取命令:
get命令 根据key获取value值,可以一次性获取多个,没有时直接返回空。
get key VALUE key 0 4 liyd END get key key1 key2 key3 VALUE key 0 4 liyd VALUE key1 0 5 liyd1 VALUE key2 0 5 liyd2 VALUE key3 0 5 liyd3 END get key4 END
gets命令 gets命令通常与cas命令一起使用,与get命令相比gets命令会额外返回一个版本号,同样可以一次获取多个值。
gets key VALUE key 0 4 13 liyd END gets key key1 key2 key3 VALUE key 0 4 13 liyd VALUE key1 0 5 14 liyd1 VALUE key2 0 5 16 liyd2 VALUE key3 0 5 15 liyd3 END
delete命令 删除数据,一次只能删除一个,如果数据不存在,返回NOT_FOUND。
delete key DELETED delete key4 NOT_FOUND
incr/decr: 如果存储的数据是一个64位的整数,可以通过incr和decr命令进行数值的增减,如果存储的值不是整数类型会报错。
set id 0 0 3 100 STORED incr id 1 101 incr id 5 106 decr id 2 104 decr id 3 101 incr key3 3 CLIENT_ERROR cannot increment or decrement non-numeric value
统计分析命令,查看memcached内部的状态,通常在调优时会用到:
stats显示进程及当前状态等信息。
stats STAT pid 3903 STAT uptime 36559 STAT time 1429569027 STAT version 1.4.14 (Ubuntu) STAT libevent 2.0.21-stable STAT pointer_size 64 STAT rusage_user 1.135569 STAT rusage_system 0.824208 STAT curr_connections 5 STAT total_connections 7 STAT connection_structures 6 STAT reserved_fds 20 STAT cmd_get 42 STAT cmd_set 32 STAT cmd_flush 0 STAT cmd_touch 0 STAT get_hits 40 STAT get_misses 2 STAT delete_misses 2 STAT delete_hits 1 STAT incr_misses 1 STAT incr_hits 4 STAT decr_misses 0 STAT decr_hits 2 STAT cas_misses 0 STAT cas_hits 1 STAT cas_badval 1 STAT touch_hits 0 STAT touch_misses 0 STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 1706 STAT bytes_written 3203 STAT limit_maxbytes 67108864 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT threads 4 STAT conn_yields 0 STAT hash_power_level 16 STAT hash_bytes 524288 STAT hash_is_expanding 0 STAT expired_unfetched 0 STAT evicted_unfetched 0 STAT bytes 444 STAT curr_items 6 STAT total_items 24 STAT evictions 0 STAT reclaimed 0 END
stats items 显示items的相关信息
stats items STAT items:1:number 6 STAT items:1:age 3890 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:1:reclaimed 0 STAT items:1:expired_unfetched 0 STAT items:1:evicted_unfetched 0 END
stats slabs 显示slab的相关信息
stats slabs STAT 1:chunk_size 96 STAT 1:chunks_per_page 10922 STAT 1:total_pages 1 STAT 1:total_chunks 10922 STAT 1:used_chunks 6 STAT 1:free_chunks 10916 STAT 1:free_chunks_end 0 STAT 1:mem_requested 444 STAT 1:get_hits 40 STAT 1:cmd_set 32 STAT 1:delete_hits 1 STAT 1:incr_hits 4 STAT 1:decr_hits 2 STAT 1:cas_hits 1 STAT 1:cas_badval 1 STAT 1:touch_hits 0 STAT active_slabs 1 STAT total_malloced 1048512 END
stats sizes 显示所有item的大小和个数
stats sizes STAT 96 6 END
旧版本可能还会有一个stats cachedump命令,用以显示slab中items的信息,在我安装的memcached 1.4.14版本中,已经不能使用该命令。
最后要说的命令是flush_all
,在使用时你可以把它当成是清空所有缓存,但实际上它只是把cache中的所有item标识为过期而已,并没有刷新和释放内存,因为memcache是懒检测机制,在get时才会检查该对象是否需要删除,这将在后面进行讲解。