Memcached命令使用

原文:http://www.cnblogs.com/czh-liyu/archive/2010/04/27/1722084.html

一、准备

1. 启动/结束memcached

1) 启动

进入安装目录bin中,执行如下代码:

[plain]  view plain copy
  1. ./memcached -d -m 200  -u root -l 10.17.0.50 -p 11211 -c 256 -P /tmp/memcached.pid  

-d start 选项是启动memcached守护进程,

-d restart 重启memcached服务

-d stop|shutdown 关闭正在运行的memcached服务

-d install 安装memcached服务

-d uninstall 卸载memcached服务

-m 是分配给Memcache使用的内存数量,单位是MB,这里是10MB (默认64MB)

-M 内存耗尽时返回错误,而不是删除项

-u 以**的身份运行(仅在以root身份运行时有效)

-l 是监听的服务器IP地址(默认是本机),如果有多个地址的话,这里指定了服务器的IP地址10.17.0.50

-p 是设置Memcache监听的端口,这里设置了11211,最好是1024以上的端口

-c 选项是最大运行的并发连接数,默认是1024,这里设置了256,按照你服务器的负载量来设定

-P 是设置保存Memcache的pid文件

-h 显示帮助信息

2) 查看memcached进程

[plain]  view plain copy
  1. ps -ef | grep memcached  

3) 结束memcached进程

[plain]  view plain copy
  1. kill -9 *****  

2. 连接/结束连接memcached

1) 连接

[plain]  view plain copy
  1. telnet localhost 11211  

如果一切正常,则应该得到一个 telnet 响应,它会指示 Connected to localhost(已经连接到 localhost)。

2) 结束连接

[plain]  view plain copy
  1. quit  

二、Memcached命令

您现现已经登录到 memcached 服务器。此后,您将能够通过一系列简单的命令来与 memcached 通信。9 个 memcached 客户端命令可以分为三类:基本高级管理

1. 基本 memcached 客户机命令

您将使用五种基本 memcached 命令执行最简单的操作。这些命令和操作包括:setaddreplacegetdelete

前三个命令是用于操作存储在 memcached 中的键值对的标准修改命令。它们都非常简单易用,且都使用如下所示的语法:

[plain]  view plain copy
  1. command <key> <flags> <expiration time> <bytes>  
  2.   
  3. <value>  

memcached 修改命令的参数和用法。

参数

用法

key

key 用于查找缓存值

flags

可以包括键值对的整型参数,客户机使用它存储关于键值对的额外信息

expiration time

在缓存中保存键值对的时间长度(以秒为单位,0 表示永远)

bytes

在缓存中存储的字节点

value

存储的值(始终位于第二行)

 

1) set

set 命令用于向缓存添加新的键值对。如果键已经存在,则之前的值将被替换。

注意以下交互,它使用了 set 命令:

[plain]  view plain copy
  1. set userId 0 0 5  
  2.   
  3. 12345  
  4.   
  5. STORED  

如果使用 set 命令正确设定了键值对,服务器将使用单词 STORED 进行响应。本示例向缓存中添加了一个键值对,其键为 userId,其值为 12345。并将过期时间设置为 0,这将向 memcached 通知您希望将此值存储在缓存中直到删除它为止。(要特别注意bytes的设置必须和value的长度相等,否则不会返回stored)

2) add

仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。如果缓存中已经存在键,则之前的值将仍然保持相同,并且您将获得响应 NOT_STORED。

下面是使用 add 命令的标准交互:

[plain]  view plain copy
  1. set userId 0 0 5  
  2.   
  3. 12345  
  4.   
  5. STORED  
  6.   
  7.    
  8.   
  9. add userId 0 0 5  
  10.   
  11. 55555  
  12.   
  13. NOT_STORED  
  14.   
  15.    
  16.   
  17. add companyId 0 0 3  
  18.   
  19. 564  
  20.   
  21. STORED  

3) replace

仅当键已经存在时,replace 命令才会替换缓存中的键。如果缓存中不存在键,那么您将从 memcached 服务器接受到一条 NOT_STORED 响应。

下面是使用 replace 命令的标准交互:

[plain]  view plain copy
  1. replace accountId 0 0 5  
  2.   
  3. 67890  
  4.   
  5. NOT_STORED  
  6.   
  7.    
  8.   
  9. set accountId 0 0 5  
  10.   
  11. 67890  
  12.   
  13. STORED  
  14.   
  15.    
  16.   
  17. replace accountId 0 0 5  
  18.   
  19. 55555  
  20.   
  21. STORED  

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

[plain]  view plain copy
  1. command <key>  

接下来看这些命令的应用。

4) get

get 命令用于检索与之前添加的键值对相关的值。您将使用 get 执行大多数检索操作。

下面是使用 get 命令的典型交互:

[plain]  view plain copy
  1. set userId 0 0 5  
  2.   
  3. 12345  
  4.   
  5. STORED  
  6.   
  7.    
  8.   
  9. get userId  
  10.   
  11. VALUE userId 0 5  
  12.   
  13. 12345  
  14.   
  15. END  
  16.   
  17.    
  18.   
  19. get bob  
  20.   
  21. END  

如您所见,get 命令相当简单。您使用一个键来调用 get,如果这个键存在于缓存中,则返回相应的值。如果不存在,则不返回任何内容。

5) delete

最后一个基本命令是 delete。delete 命令用于删除 memcached 中的任何现有值。您将使用一个键调用 delete,如果该键存在于缓存中,则删除该值。如果不存在,则返回一条NOT_FOUND 消息。

下面是使用 delete 命令的客户机服务器交互:

[plain]  view plain copy
  1. set userId 0 0 5  
  2.   
  3. 98765  
  4.   
  5. STORED  
  6.   
  7.    
  8.   
  9. delete bob  
  10.   
  11. NOT_FOUND  
  12.   
  13.    
  14.   
  15. delete userId  
  16.   
  17. DELETED  
  18.   
  19.    
  20.   
  21. get userId  
  22.   
  23. END  

2. 高级 memcached 客户机命令

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

1) gets

gets 命令的功能类似于基本的 get 命令。两个命令之间的差异在于,gets 返回的信息稍微多一些:64 位的整型值非常像名称/值对的“版本”标识符。

下面是使用 gets 命令的客户机服务器交互:

[plain]  view plain copy
  1. set userId 0 0 5  
  2.   
  3. 12345  
  4.   
  5. STORED  
  6.   
  7.    
  8.   
  9. get userId  
  10.   
  11. VALUE userId 0 5  
  12.   
  13. 12345  
  14.   
  15. END  
  16.   
  17.    
  18.   
  19. gets userId  
  20.   
  21. VALUE userId 0 5 4  
  22.   
  23. 12345  
  24.   
  25. END  

考虑 get 和 gets 命令之间的差异。gets 命令将返回一个额外的值 — 在本例中是整型值 4,用于标识名称/值对。如果对此名称/值对执行另一个 set 命令,则 gets 返回的额外值将会发生更改,以表明名称/值对已经被更新。

[plain]  view plain copy
  1. set userId 0 0 5  
  2.   
  3. 33333  
  4.   
  5. STORED  
  6.   
  7.    
  8.   
  9. gets userId  
  10.   
  11. VALUE userId 0 5 5  
  12.   
  13. 33333  
  14.   
  15. END  

您看到 gets 返回的值了吗?它已经更新为 5。您每次修改名称/值对时,该值都会发生更改。

2) cas

cas(check 和 set)是一个非常便捷的 memcached 命令,用于设置名称/值对的值(如果该名称/值对在您上次执行 gets 后没有更新过)。它使用与 set 命令相类似的语法,但包括一个额外的值:gets 返回的额外值。

注意以下使用 cas 命令的交互:

[plain]  view plain copy
  1. set userId 0 0 5  
  2.   
  3. 55555  
  4.   
  5. STORED  
  6.   
  7.    
  8.   
  9. gets userId  
  10.   
  11. VALUE userId 0 5 6  
  12.   
  13. 55555  
  14.   
  15. END  
  16.   
  17.    
  18.   
  19. cas userId 0 0 5 6  
  20.   
  21. 33333  
  22.   
  23. STORED  

如您所见,我使用额外的整型值 6 来调用 gets 命令,并且操作运行非常顺序。

使用旧版本指示符的 cas 命令

[plain]  view plain copy
  1. set userId 0 0 5  
  2.   
  3. 55555  
  4.   
  5. STORED  
  6.   
  7.    
  8.   
  9. gets userId  
  10.   
  11. VALUE userId 0 5 8  
  12.   
  13. 55555  
  14.   
  15. END  
  16.   
  17.    
  18.   
  19. cas userId 0 0 5 6  
  20.   
  21. 33333  
  22.   
  23. EXISTS  

注意,我并未使用 gets 最近返回的整型值,并且 cas 命令返回 EXISTS 值以示失败。从本质上说,同时使用 gets 和 cas 命令可以防止您使用自上次读取后经过更新的名称/值对。

3. 缓存管理命令

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

 

1) stats

stats 命令的功能正如其名:转储所连接的 memcached 实例的当前统计数据。在下例中,执行 stats 命令显示了关于当前 memcached 实例的信息:

[plain]  view plain copy
  1. stats  
  2.   
  3. STAT pid 21291  
  4.   
  5. STAT uptime 4987  
  6.   
  7. STAT time 1402996745  
  8.   
  9. STAT version 1.4.20  
  10.   
  11. STAT libevent 2.0.21-stable  
  12.   
  13. STAT pointer_size 64  
  14.   
  15. STAT rusage_user 0.022996  
  16.   
  17. STAT rusage_system 0.206968  
  18.   
  19. STAT curr_connections 5  
  20.   
  21. STAT total_connections 7  
  22.   
  23. STAT connection_structures 6  
  24.   
  25. STAT reserved_fds 20  
  26.   
  27. STAT cmd_get 0  
  28.   
  29. STAT cmd_set 0  
  30.   
  31. STAT cmd_flush 0  
  32.   
  33. STAT cmd_touch 0  
  34.   
  35. STAT get_hits 0  
  36.   
  37. STAT get_misses 0  
  38.   
  39. STAT delete_misses 0  
  40.   
  41. STAT delete_hits 0  
  42.   
  43. STAT incr_misses 0  
  44.   
  45. STAT incr_hits 0  
  46.   
  47. STAT decr_misses 0  
  48.   
  49. STAT decr_hits 0  
  50.   
  51. STAT cas_misses 0  
  52.   
  53. STAT cas_hits 0  
  54.   
  55. STAT cas_badval 0  
  56.   
  57. STAT touch_hits 0  
  58.   
  59. STAT touch_misses 0  
  60.   
  61. STAT auth_cmds 0  
  62.   
  63. STAT auth_errors 0  
  64.   
  65. STAT bytes_read 28  
  66.   
  67. STAT bytes_written 1076  
  68.   
  69. STAT limit_maxbytes 209715200  
  70.   
  71. STAT accepting_conns 1  
  72.   
  73. STAT listen_disabled_num 0  
  74.   
  75. STAT threads 4  
  76.   
  77. STAT conn_yields 0  
  78.   
  79. STAT hash_power_level 16  
  80.   
  81. STAT hash_bytes 524288  
  82.   
  83. STAT hash_is_expanding 0  
  84.   
  85. STAT malloc_fails 0  
  86.   
  87. STAT bytes 0  
  88.   
  89. STAT curr_items 0  
  90.   
  91. STAT total_items 0  
  92.   
  93. STAT expired_unfetched 0  
  94.   
  95. STAT evicted_unfetched 0  
  96.   
  97. STAT evictions 0  
  98.   
  99. STAT reclaimed 0  
  100.   
  101. STAT crawler_reclaimed 0  
  102.   
  103. END  


此处的大多数输出都非常容易理解。

pid: memcached服务进程的进程ID

uptime: memcached服务从启动到当前所经过的时间,单位是秒。

time: memcached服务器所在主机当前系统的时间,单位是秒。

version: memcached组件的版本。这里是我当前使用的1.2.6。

pointer_size:服务器所在主机操作系统的指针大小,一般为32或64.

curr_items:表示当前缓存中存放的所有缓存对象的数量。不包括目前已经从缓存中删除的对象。

total_items:表示从memcached服务启动到当前时间,系统存储过的所有对象的数量,包括目前已经从缓存中删除的对象。

 

bytes:表示系统存储缓存对象所使用的存储空间,单位为字节。

curr_connections:表示当前系统打开的连接数。

total_connections:表示从memcached服务启动到当前时间,系统打开过的连接的总数。

connection_structures:表示从memcached服务启动到当前时间,被服务器分配的连接结构的数量,这个解释是协议文档给的,具体什么意思,我目前还没搞明白。

cmd_get:累积获取数据的数量,这里是3,因为我测试过3次,第一次因为没有序列化对象,所以获取数据失败,是null,后边有2次是我用不同对象测试了2次。

cmd_set:累积保存数据的树立数量,这里是2.虽然我存储了3次,但是第一次因为没有序列化,所以没有保存到缓存,也就没有记录。

get_hits:表示获取数据成功的次数。

get_misses:表示获取数据失败的次数。

evictions:为了给新的数据项目释放空间,从缓存移除的缓存对象的数目。比如超过缓存大小时根据LRU算法移除的对象,以及过期的对象。

bytes_read:memcached服务器从网络读取的总的字节数。

bytes_written:memcached服务器发送到网络的总的字节数。

limit_maxbytes:memcached服务缓存允许使用的最大字节数。这里为67108864字节,也就是是64M.与我们启动memcached服务设置的大小一致。

threads:被请求的工作线程的总数量。这个解释是协议文档给的,具体什么意思,我目前还没搞明白。

 

2) flush_all

flush_all 是最后一个要介绍的命令。这个最简单的命令仅用于清理缓存中的所有名称/值对。如果您需要将缓存重置到干净的状态,则 flush_all 能提供很大的用处。下面是一个使用 flush_all 的例子:

[plain]  view plain copy
  1. set userId 0 0 5  
  2.   
  3. 55555  
  4.   
  5. STORED  
  6.   
  7.    
  8.   
  9. get userId  
  10.   
  11. VALUE userId 0 5  
  12.   
  13. 55555  
  14.   
  15. END  
  16.   
  17.    
  18.   
  19. flush_all  
  20.   
  21. OK  
  22.   
  23.    
  24.   
  25. get userId  
  26.   
  27. END  

4. 缓存性能

在本文的最后,我将讨论如何使用高级 memcached 命令来确定缓存的性能。stats 命令用于调优缓存的使用。需要注意的两个最重要的统计数据是 et_hits 和 get_misses。这两个值分别指示找到名称/值对的次数(get_hits)和未找到名称/值对的次数(get_misses)。

结合这些值,我们可以确定缓存的利用率如何。初次启动缓存时,可以看到 get_misses 会自然地增加,但在经过一定的使用量之后,这些 get_misses 值应该会逐渐趋于平稳 — 这表示缓存主要用于常见的读取操作。如果您看到 get_misses 继续快速增加,而 get_hits 逐渐趋于平稳,则需要确定一下所缓存的内容是什么。您可能缓存了错误的内容。

确定缓存效率的另一种方法是查看缓存的命中率(hit ratio)。缓存命中率表示执行 get 的次数与错过 get 的次数的百分比。要确定这个百分比,需要再次运行 stats 命令,如所示:

计算缓存命中率

[plain]  view plain copy
  1. stats  
  2.   
  3. STAT pid 21291  
  4.   
  5. STAT uptime 4987  
  6.   
  7. STAT time 1402996745  
  8.   
  9. STAT version 1.4.20  
  10.   
  11. STAT libevent 2.0.21-stable  
  12.   
  13. STAT pointer_size 64  
  14.   
  15. STAT rusage_user 0.022996  
  16.   
  17. STAT rusage_system 0.206968  
  18.   
  19. STAT curr_connections 5  
  20.   
  21. STAT total_connections 7  
  22.   
  23. STAT connection_structures 6  
  24.   
  25. STAT reserved_fds 20  
  26.   
  27. STAT cmd_get 100  
  28.   
  29. STAT cmd_set 68  
  30.   
  31. STAT cmd_flush 0  
  32.   
  33. STAT cmd_touch 0  
  34.   
  35. STAT get_hits 50  
  36.   
  37. STAT get_misses 45  
  38.   
  39. STAT delete_misses 0  
  40.   
  41. STAT delete_hits 0  
  42.   
  43. STAT incr_misses 0  
  44.   
  45. STAT incr_hits 0  
  46.   
  47. STAT decr_misses 0  
  48.   
  49. STAT decr_hits 0  
  50.   
  51. STAT cas_misses 0  
  52.   
  53. STAT cas_hits 0  
  54.   
  55. STAT cas_badval 0  
  56.   
  57. STAT touch_hits 0  
  58.   
  59. STAT touch_misses 0  
  60.   
  61. STAT auth_cmds 0  
  62.   
  63. STAT auth_errors 0  
  64.   
  65. STAT bytes_read 28  
  66.   
  67. STAT bytes_written 1076  
  68.   
  69. STAT limit_maxbytes 209715200  
  70.   
  71. STAT accepting_conns 1  
  72.   
  73. STAT listen_disabled_num 0  
  74.   
  75. STAT threads 4  
  76.   
  77. STAT conn_yields 0  
  78.   
  79. STAT hash_power_level 16  
  80.   
  81. STAT hash_bytes 524288  
  82.   
  83. STAT hash_is_expanding 0  
  84.   
  85. STAT malloc_fails 0  
  86.   
  87. STAT bytes 0  
  88.   
  89. STAT curr_items 0  
  90.   
  91. STAT total_items 0  
  92.   
  93. STAT expired_unfetched 0  
  94.   
  95. STAT evicted_unfetched 0  
  96.   
  97. STAT evictions 0  
  98.   
  99. STAT reclaimed 0  
  100.   
  101. STAT crawler_reclaimed 0  
  102.   
  103. END  

现在,用 get_hits 的数值除以 cmd_gets。在本例中,您的命中率大约是 50%。在理想情况下,您可能希望得到更高的百分比 — 比率越高越好。查看统计数据并不时测量它们可以很好地判定缓存策略的效率。


对查看的信息的关键字中英文对照表

pid

memcache服务器的进程ID

uptime

服务器已经运行的秒数

time

服务器当前的unix时间戳

version

memcache版本

pointer_size

当前操作系统的指针大小(32位系统一般是32bit)

rusage_user

进程的累计用户时间

rusage_system

进程的累计系统时间

curr_items

服务器当前存储的items数量

total_items

从服务器启动以后存储的items总数量

bytes

当前服务器存储items占用的字节数

curr_connections

当前打开着的连接数

total_connections

从服务器启动以后曾经打开过的连接数

connection_structures

服务器分配的连接构造数

cmd_get

get命令(获取)总请求次数

cmd_set

set命令(保存)总请求次数

get_hits

总命中次数

get_misses

总未命中次数

evictions

为获取空闲内存而删除的items数(分配给memcache的空间用满后需要删除旧的items来得到空间分配给新的items)

bytes_read

总读取字节数(请求字节数)

bytes_written

总发送字节数(结果字节数)

limit_maxbytes

分配给memcache的内存大小(字节)

threads

当前线程数



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值