一、调试参数
-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
<command> <key> <flags> <exptime> <bytes> [<version>]\r\n
<datablock>\r\n
<status>\r\n
command | set无论如何都进行存储 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 |
bytes | byte字节数,不包含\r\n,根据长度截取存/取的字符串,可以是0,即存空串 |
datablock | 文本行,以\r\n结尾,当然可以包含\r或\n |
status | STORED/NOT_STORED/EXISTS/NOT_FOUND ERROR/CLIENT_ERROR/SERVER_ERROR服务端会关闭连接以修复 |
a. datablock 长度必须正确
- set liu 32 0 4
- java
- STORED//正确
- get liu
- VALUE abc 32 4
- java
- END
- set liu 32 0 4
- cplus
- CLIENT_ERROR bad data chunk
- ERROR//长度错误
b. add 只能添加不存在的key
- set liu 32 0 4
- java
- STORED
- add liu 32 0 5
- cplus
- NOT_STORED
- //已存在不能add
- get liu
- VALUE abc 32 4
- java
- END
- add song 32 0 5
- cplus
- STORED
- //不存在可以add
c. replace只能替换已有的key
- set liu 32 0 4
- java
- STORED
- replace liu 32 0 5
- cplus
- STORED
- //已存在可以replace
- get liu
- VALUE cplus 32 5
- liu
- END
- replace yang 32 0 5
- cplus
- NOT_STORED
- //不存在不能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. 查询多个键值
- <span style="color:#333333;">get liu song yang
- VALUE liu 32 4
- java
- VALUE song 32 5
- cplus
- END</span>
b. 查询带版本的值
- <span style="color:#333333;">gets liu
- VALUE liu 32 4 12
- java
- END
- //取得版本号
- replace liu 32 0 4
- java
- STORED
- //增加版本号
- get liu
- VALUE liu 32 4
- java
- END
- gets liu
- VALUE liu 32 4 13
- java
- END</span>
4.监察存储命令cas
- cas liu 32 0 5 12
- cplus
- EXISTS
- gets liu
- VALUE liu 32 4 13
- java
- END//版本号不同不修改
- cas liu 32 0 5 13
- cplus
- STORED
- gets liu
- VALUE liu 32 5 14
- cplus
- END//版本号相同才修改
cas即check and set,只有版本号相匹配时才能存储,否则返回EXISTS
设计意图:解决多客户端并发修改同一条记录的问题,防止使用经过改变了的value/key对
a. 实现计数器
- <span style="color:#333333;">set count 32 0 1
- 1
- STORED
- incr count 8
- 9
- decr count 2
- 7</span>
b. key不存在不能计数
- <span style="color: rgb(51, 51, 51); ">delete count
- DELETED
- incr count 1
- NOT_FOUND</span>
c. value不是数字不能计数
- <span style="color: rgb(51, 51, 51); ">incr liu 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
delete <key> [<time>]
DELETE\r\n
time: 秒数或Unixtime,在time时间内不能add或replace,但能set,不能get。过期后才能够重新set有效并能get
- delete liu
- DELETED
- get liu