Memcached 连接
用 nc / telnet 连 memcached,理解文本协议。
Key PointsMemcached 用极简文本行协议:一行命令、一行数据、一行响应,肉眼可读。
用 nc 连接
Shell
# 进入交互:每行输入一条命令,回车看响应
nc 127.0.0.1 11211Output
(连接成功,光标停在新行,等待输入命令)协议长这样
Shell
set foo 0 900 5
helloOutput
STOREDset foo 0 900 5: command, key, flag, TTL 900s, value length 5; the next line is the 5-byte value hello. Server replies STORED.
flag is an opaque integer for the client (e.g. marking encoding); memcached stores it untouched and returns it with the value. TTL 0 means 'never expire' (still evictable by LRU).
用 nc 连接
Shell
# 进入交互:每行输入一条命令,回车看响应
nc 127.0.0.1 11211Output
(连接成功,光标停在新行,等待输入命令)协议长这样
Shell
set foo 0 900 5
helloOutput
STOREDset foo 0 900 5 五段含义:命令 set、key foo、flag 0、过期 900 秒、值长度 5 字节;下一行是正好 5 字节的值 hello。服务端回 STORED 表示存好。
flag 是给客户端自定义用的整数(比如标记值的编码/类型),memcached 不解释、原样随值返回。过期时间 0 表示永不过期(仍会被 LRU 挤掉)。