Memcached调试参数&常用命令

一、调试参数

      -v +输出error/warning

     -vv +输出命令/响应

     -vvv +输出内部状态


存储命令   set/add/replace/append/prepend/cas、

读取命令   get=bget?/gets

删除命令   delete

计数命令   incr/decr

统计命令     stats/settings/items/sizes/slabs

工具           memcached-tool


1.存储命令

格式:
<command> <key> <flags> <exptime> <bytes> [<version>]\r\n
<datablock>\r\n
<status>\r\n

commandset无论如何都进行存储
add只有数据不存在时进行添加
repalce只有数据存在时进行替换
append往后追加:append <key> datablock <status>
prepend往前追加:prepend <key> datablock <status>

cas按版本号更改
key字符串,<250个字符,不包含空格和控制字符
flags客户端用来标识数据格式的数值,如json,xml,压缩等
exptime存活时间 s 以秒为单位,0为永远,<30天60*60*24*30为秒数,>30天为unixtime
bytesbyte字节数,不包含\r\n,根据长度截取存/取的字符串,可以是0,即存空串
datablock文本行,以\r\n结尾,当然可以包含\r或\n
statusSTORED/NOT_STORED/EXISTS/NOT_FOUND
ERROR/CLIENT_ERROR/SERVER_ERROR服务端会关闭连接以修复
















a.  datablock 长度必须正确

[html]  view plain copy
  1. set liu 32 0 4  
  2. java  
  3. STORED//正确  
  4.   
  5. get liu  
  6. VALUE abc 32 4  
  7. java  
  8. END  
  9.   
  10. set liu 32 0 4  
  11. cplus  
  12. CLIENT_ERROR bad data chunk  
  13. ERROR//长度错误  

 b.  add 只能添加不存在的key

[html]  view plain copy
  1. set liu 32 0 4  
  2. java  
  3. STORED  
  4.   
  5. add liu 32 0 5  
  6. cplus  
  7. NOT_STORED  
  8. //已存在不能add  
  9.   
  10. get liu  
  11. VALUE abc 32 4  
  12. java  
  13. END  
  14.   
  15. add song 32 0 5  
  16. cplus  
  17. STORED  
  18. //不存在可以add  

c.  replace只能替换已有的key

[html]  view plain copy
  1. set liu 32 0 4  
  2. java  
  3. STORED  
  4.   
  5. replace liu 32 0 5  
  6. cplus  
  7. STORED  
  8. //已存在可以replace  
  9.   
  10. get  liu  
  11. VALUE cplus 32 5  
  12. liu  
  13. END  
  14.   
  15. replace yang 32 0 5  
  16. cplus  
  17. NOT_STORED  
  18. //不存在不能replace  

2.读取命令

格式:
<command> <key>*\r\n
VALUE <key1> <flags> <bytes> [<version>]\r\n
<datablock>\r\n

VALUE <keyn> <flags> <bytes> [<version>]\r\n
<datablock>\r\n
END\r\n

command:  get普通查询,gets用于查询带版本的值

a.  查询多个键值

[html]  view plain copy
  1. <span style="color:#333333;">get liu song yang  
  2. VALUE liu 32 4  
  3. java  
  4. VALUE song 32 5  
  5. cplus  
  6. END</span>  

b.  查询带版本的值

[html]  view plain copy
  1. <span style="color:#333333;">gets liu  
  2. VALUE liu 32 4 12  
  3. java  
  4. END  
  5. //取得版本号  
  6.   
  7.   
  8. replace liu 32 0 4  
  9. java  
  10. STORED  
  11. //增加版本号  
  12.   
  13.   
  14. get liu  
  15. VALUE liu 32 4  
  16. java  
  17. END  
  18.   
  19.   
  20. gets liu  
  21. VALUE liu 32 4 13  
  22. java  
  23. END</span>  

4.监察存储命令cas

[html]  view plain copy
  1. cas liu 32 0 5 12  
  2. cplus  
  3. EXISTS  
  4.   
  5. gets liu  
  6. VALUE liu 32 4 13  
  7. java  
  8. END//版本号不同不修改  
  9.   
  10. cas liu 32 0 5 13  
  11. cplus  
  12. STORED  
  13.   
  14. gets liu  
  15. VALUE liu 32 5 14  
  16. cplus  
  17. END//版本号相同才修改  

cas即check and set,只有版本号相匹配时才能存储,否则返回EXISTS
设计意图:解决多客户端并发修改同一条记录的问题,防止使用经过改变了的value/key对

5.计数命令 incr/decr

格式:
incr/decr<key> <int>
<int>
要求:
key必须存在,value必须是数字

a.  实现计数器

[html]  view plain copy
  1. <span style="color:#333333;">set count 32 0 1    
  2. 1  
  3. STORED  
  4.   
  5.   
  6. incr count 8  
  7. 9  
  8.   
  9.   
  10. decr count 2  
  11. 7</span>  

b.  key不存在不能计数

[html]  view plain copy
  1. <span style="color: rgb(51, 51, 51); ">delete count   
  2. DELETED  
  3.   
  4.   
  5. incr count 1  
  6. NOT_FOUND</span>  

c.  value不是数字不能计数

[html]  view plain copy
  1. <span style="color: rgb(51, 51, 51); ">incr liu 2  
  2. CLIENT_ERROR cannot increment or decrement non-numeric value</span>  

6.删除命令delete

格式:
delete <key> [<time>]
DELETE\r\n


time: 秒数或Unixtime,在time时间内不能add或replace,但能set,不能get。过期后才能够重新set有效并能get
[html]  view plain copy
  1. delete liu  
  2. DELETED  
  3. get liu  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值