memcached协议及命令

来源:互联网 发布:io域名注册 编辑:程序博客网 时间:2024/05/20 14:20

搜索 整理以后内容。


memcached协议及命令
基本 memcached 客户机命令

五种基本 memcached 命令执行最简单的操作:
set
add
replace
get
delete

set add replace用于操作存储在 memcached 中的键值对的标准修改命令。
它们都非常简单易用,使用的语法:
修改命令语法: command <key> <flags> <expiration time> <bytes> <value>

memcached 修改命令参数说明:
参数 用法
key 用于查找缓存值
flags 可以包括键值对的整型参数,客户机使用它存储关于键值对的额外信息
expiration time 在缓存中保存键值对的时间长度(以秒为单位,0 表示永远)
bytes 在缓存中存储的字节点
value 存储的值(始终位于第二行)

实际使用
set
set 命令用于向缓存添加新的键值对。
如果键已经存在,则之前的值将被替换。
如果使用 set 命令正确设定了键值对,服务器将使用单词 STORED 进行响应。
如:
roothomes$ telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set key_roothomes 0 0 13
roothomes
STORED
get key_roothomes
VALUE key_roothomes 0 13
roothomes
END
quit
Connection closed by foreign host.

本示例向缓存中添加了一个键值对,其键为 key_roothomes ,其值为 roothomes。
并将过期时间设置为 0,这将向 memcached 通知您希望将此值存储在缓存中直到删除它为止。

add
仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。
如果缓存中已经存在键,则之前的值将仍然保持相同,并且您将获得响应 NOT_STORED 。
如:
roothomes$ telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get roothomes
END
add key_roothomes 0 0 13
roothomes
NOT_STORED
add key_roothomes1 0 0 14
roothomes1
STORED
quit
Connection closed by foreign host.

replace
仅当键已经存在时,replace 命令才会替换缓存中的键。
如果缓存中不存在键,那么您将从 memcached 服务器接受到一条 NOT_STORED 响应。
如:
roothomes$ telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
replace key_roothomes2 0 0 10
1234567890
NOT_STORED
replace key_roothomes 0 0 10
1234567890
STORED
get key_roothomes
VALUE key_roothomes 0 10
1234567890
END
quit
Connection closed by foreign host.


最后两个基本命令是 get 和 delete。
这些命令相当容易理解,并且使用了类似的语法,
如下所示:command <key>

get
get 命令用于检索与之前添加的键值对相关的值。
您将使用 get 执行大多数检索操作。
get 命令相当简单,get 键。
如果这个键存在于缓存中,则返回相应的值。如果不存在,则不返回任何内容。

delete
最后一个基本命令是 delete。delete 命令用于删除 memcached 中的任何现有值。您将使用一个键调用 delete,如果该键存在于缓存中,则删除该值。如果不存在,则返回一条 NOT_FOUND 消息。
如:
roothomes$ telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key_roothomes10
END
get key_roothomes
VALUE key_roothomes 0 10
1234567890
END
delete key_roothomes3
NOT_FOUND
delete key_roothomes1
DELETED
quit
Connection closed by foreign host.


高级 memcached 客户机命令

可以在 memcached 中使用的两个高级命令是 gets 和 cas。gets 和 cas 命令需要结合使用。
您将使用这两个命令来确保不会将现有的名称/值对设置为新值(如果该值已经更新过)。

gets
gets 命令的功能类似于get 命令。差异在 gets 返回的信息稍微多一些:64 位的整型值非常像名称/值对的 “版本” 标识符。
roothomes$ telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
gets key_roothomes
VALUE key_roothomes 0 10 3
1234567890
END
get key_roothomes
VALUE key_roothomes 0 10
1234567890
END
quit
Connection closed by foreign host.

cas
cas(check 和 set)是一个非常便捷的 memcached 命令,用于设置名称/值对的值(如果该名称/值对在您上次执行 gets 后没有更新过)。
它使用与 set命令相类似的语法,但包括一个额外的值:gets 返回的额外值。
cas命令和set命令类似,不同的是set没有指定现在key的版本号,如果版本号对不上就不能更新该健的内容。
如:
roothomes$ telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
gets key_roothomes
VALUE key_roothomes 0 10 7
00000000
END
cas key_roothomes 0 0 10 8
88888888
EXISTS
set key_roothomes 0 0 10
66666666
STORED
quit
Connection closed by foreign host.

缓存管理命令

最后两个 memcached 命令用于监控和清理 memcached 实例。
它们是 stats 和 flush_all 命令。

stats
stats 命令的功能正如其名:转储所连接的 memcached 实例的当前统计数据。
在下例是memcached的一个stats的信息:
roothomes$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
STAT pid 26608
STAT uptime 3798
STAT time 1320813973
STAT version 1.2.6
STAT pointer_size 32
STAT rusage_user 0.000000
STAT rusage_system 0.000000
STAT curr_items 2
STAT total_items 3
STAT bytes 112
STAT curr_connections 3
STAT total_connections 5
STAT connection_structures 4
STAT cmd_get 8
STAT cmd_set 3
STAT get_hits 2
STAT get_misses 6
STAT evictions 0
STAT bytes_read 243
STAT bytes_written 126
STAT limit_maxbytes 10485760
STAT threads 1
END
quit
Connection closed by foreign host.

此处的大多数输出都非常容易理解。
稍后在讨论缓存性能时,我还将详细解释这些值的含义。
至于目前,我们先来看看输出,然后再使用新的键来运行一些 set命令,并再次运行 stats 命令,注意发生了哪些变化。

flush_all
flush_all 是最后一个要介绍的命令。这个最简单的命令仅用于清理缓存中的所有名称/值对。
如果您需要将缓存重置到干净的状态,则 flush_all 能提供很大的用处。
在下例是memcached的一个flush_all的信息:
roothomes$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
flush_all
OK
quit
Connection closed by foreign host.


缓存性能

在本文的最后,我将讨论如何使用高级 memcached 命令来确定缓存的性能。stats 命令用于调优缓存的使用。需要注意的两个最重要的统计数据是 et_hits 和 get_misses。这两个值分别指示找到名称/值对的次数(get_hits)和未找到名称/值对的次数(get_misses)。
结合这些值,我们可以确定缓存的利用率如何。初次启动缓存时,可以看到 get_misses 会自然地增加,但在经过一定的使用量之后,这些 get_misses 值应该会逐渐趋于平稳 — 这表示缓存主要用于常见的读取操作。如果您看到 get_misses 继续快速增加,而 get_hits 逐渐趋于平稳,则需要确定一下所缓存的内容是什么。您可能缓存了错误的内容。

确定缓存效率的另一种方法是查看缓存的命中率(hit ratio)。缓存命中率表示执行 get 的次数与错过 get 的次数的百分比。要确定这个百分比,需要再次运行 stats 命令,如清单 8 所示:
计算缓存命中率,get_hits/cmd_get 的值。
现在,用 get_hits 的数值除以 cmd_get。在本例中,您的命中率大约是 25%。
在理想情况下,您可能希望得到更高的百分比 — 比率越高越好。查看统计数据并不时测量它们可以很好地判定缓存策略的效率。

原创粉丝点击