Memcached Protocol
8 methodsMemcached 文本协议命令集,用于键值存储与缓存操作。
set <key> <flags> <exptime> <bytes>存储键值对,无论键是否存在均写入。
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | 缓存键名。 |
| flags | number | 客户端自定义标志位(整数)。 |
| exptime | number | 过期时间(秒),0 表示永不过期。 |
| bytes | number | 数据块字节数。 |
Returns
成功返回 STORED,失败返回 NOT_STORED/CLIENT_ERROR。
Example
memcached
set foo 0 0 3
bar
# STORED
set user:1 0 3600 13
{"name":"Tom"}
# STOREDget <key>读取一个或多个键对应的值。
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | 一个或多个缓存键名,空格分隔。 |
Returns
返回 VALUE <key> <flags> <bytes> 与数据块,以 END 结束。
Example
memcached
get foo
# VALUE foo 0 3
# bar
# END
get user:1 user:2add <key> <flags> <exptime> <bytes>仅当键不存在时才存储新值。
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | 缓存键名。 |
| flags | number | 客户端自定义标志位。 |
| exptime | number | 过期时间(秒)。 |
| bytes | number | 数据块字节数。 |
Returns
成功返回 STORED,键已存在返回 NOT_STORED。
Example
memcached
add counter 0 0 1
1
# STORED
add counter 0 0 1
2
# NOT_STOREDreplace <key> <flags> <exptime> <bytes>仅当键已存在时才替换其值。
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | 缓存键名。 |
| flags | number | 客户端自定义标志位。 |
| exptime | number | 过期时间(秒)。 |
| bytes | number | 数据块字节数。 |
Returns
成功返回 STORED,键不存在返回 NOT_STORED。
Example
memcached
replace counter 0 0 1
5
# STORED
replace missing 0 0 1
x
# NOT_STOREDappend <key> <flags> <exptime> <bytes>将数据追加到现有键值末尾,不修改 flags。
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | 缓存键名。 |
| flags | number | 客户端标志位(通常被忽略)。 |
| exptime | number | 过期时间(秒)。 |
| bytes | number | 追加数据块字节数。 |
Returns
成功返回 STORED,键不存在返回 NOT_STORED。
Example
memcached
set msg 0 0 5
Hello
# STORED
append msg 0 0 6
World
# STORED
get msg
# VALUE msg 0 11
# Hello World
# ENDcas <key> <flags> <exptime> <bytes> <casunique>比较并交换:仅当 casunique 与服务器端令牌一致时才写入。
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | 缓存键名。 |
| flags | number | 客户端自定义标志位。 |
| exptime | number | 过期时间(秒)。 |
| bytes | number | 数据块字节数。 |
| casunique | number | 由 gets 获取的唯一令牌。 |
Returns
成功返回 STORED;令牌不匹配返回 EXISTS;键不存在返回 NOT_FOUND。
Example
memcached
gets foo
# VALUE foo 0 3 12
# bar
# END
cas foo 0 0 3 12
baz
# STORED
cas foo 0 0 3 99
qux
# EXISTSincr <key> <value>将键对应的数值自增指定量,键不存在返回 NOT_FOUND。
Parameters
| Name | Type | Description |
|---|---|---|
| key | string | 缓存键名,值须为十进制数字。 |
| value | number | 要增加的正整数。 |
Returns
返回自增后的数值字符串;非数值或键不存在时返回错误。
Example
memcached
set counter 0 0 1
1
# STORED
incr counter 1
# 2
incr counter 5
# 7stats查询服务器统计信息,如连接数、命中率、内存使用等。
Returns
返回多行 STAT <name> <value>,以 END 结束。
Example
memcached
stats
# STAT pid 12345
# STAT uptime 3600
# STAT curr_connections 10
# STAT get_hits 1000
# STAT get_misses 50
# END
stats items
stats slabs