---
title: "查看、分析memcached使用状态"
author: "ssxxjjii"
date: "2011-12-18T21:02:57Z"
tags: []
categories: ["Web前端"]
slug: "memcached"
draft: false
lastmod: "2011-12-18T21:02:57Z"
id: 1315706
url: "https://tdcq.iteye.com/blog/1315706"
---

访问量上升，数据库压力大，怎么办？好办法是在中间挡一层缓存！这个缓存要求高效，不能比数据库慢，否则服务质量受影响；如果能把数据用hash打散存储到硬盘，也是可以的，不过在内存越来越便宜的今天，还是使用内存吧！

    mysql也有自己的缓存，也是存储在内存的，但是有一个说法是：

以下是引用片段：

只能有一个实例  
意味着你能存储内容的上限就是你服务器的可用内存，一台服务器能有多少内存？你又能存多少呢？

只要有写操作，mysql的query cache就失效  
只要数据库内容稍有改变，那怕改变的是其他行，mysql的query cache也会失效

    再说，如果mysql都抗不住了，怎么还能指望它提供的缓存呢？

    所以我可以使用memcached了！他的好处和如何用可以参考：

以下是引用片段：

1：《[Memcache和mysql交互流程操作原理](http://www.ourmysql.com/archives/415)》

2：《[让memcached和mysql更好的工作](http://www.ourmysql.com/archives/195)》

    开发时面对需求是个麻烦事，更漫长而闹心的是维护，所以我更关心的是memcached运行中的情况。还好的是，memcached的作者给我们提供查看运行情况的命令。主要是“stats”，使用方法为 “telnet ip 端口号”，登录后使用“stats”命令。

    然后你可以看见很多内容，具体可以参考：《[memcacche stats](http://lists.danga.com/pipermail/memcached/2006-March/002065.html)》

以下是引用片段：

pid = process id  
uptime = number of seconds since the process was started  
time = current time  
version = memcached version  
rusage\_user = seconds the cpu has devoted to the process as the user  
rusage\_system = seconds the cpu has devoted to the process as the system  
curr\_items = total number of items currently in memcache  
total\_items = total number of items that have passed through the cache  
bytes = total number of bytes currently in use by curr\_items  
curr\_connections = total number of open connections to memcached  
connection\_structures = ???  
cmd\_get = total GET commands issued to the server  
cmd\_set = total SET commands issued to the server  
get\_hits = total number of times a GET command was able to retrieve and  
return data  
get\_misses = total number of times a GET command was unable to retrieve and  
return data  
bytes\_read = total number of bytes input into the server  
bytes\_written = total number of bytes written by the server  
limit\_maxbytes = total storage bytes available to the server.

    着重说一下几个对观测很有用的项。

    **limit\_maxbytes、bytes**

    memcached在存储的时候是可以设置失效时间的，但如果存储已经满了，那旧数据即使没有到过期时间，也会被移除。所以需要观察memcached存储是否已经满了，同时这对扩容也是有意义的参考。limit\_maxbytes即总的存储大小，而bytes就是已经使用的大小，从这两个数据就可以看出在memcached启动时，我们为它分配的内存是否足够使用。

    **cmd\_get、cmd\_set**

    memcached启动后，我们对它一共做了多少次读取操作呢？从这两个参数可以观察出来。

    **get\_hits、get\_misses**

    使用memcached后，我们需要评估我们使用的策略是否合理。不能够使用中间缓存后，后端的数据库还是有较大的访问量，这样的话中间缓存就变得没有意义了。get\_hits表示命中了多少次读取，即来memcached取到了多少有效数据；get\_misses表示没有命中的次数，即此次来取数据的时候，memcached并没有你所查询的数据。如果没有清零统计数据的话，cmd\_get = get\_hits + get\_misses。

    memcached 的状态查询还有其它的命令，可以参考：《[Memcached的stats命令](http://blog.alwaysmylove.net/2008/04/10/stats-command-in-memcached/)》

    如下：

stats reset  
清空统计数据

stats malloc  
显示内存分配数据

stats maps  
这个不太确定，看源代码是把/proc/self/maps的数据显示出来。

stats cachedump slab\_id limit\_num  
显示某个slab中的前limit\_num个key列表，显示格式如下  
ITEM key\_name \[ value\_length b; expire\_time|access\_time s]  
其中，memcached 1.2.2及以前版本显示的是  访问时间(timestamp)  
1.2.4以上版本，包括1.2.4显示 过期时间(timestamp)  
如果是永不过期的key，expire\_time会显示为服务器启动的时间

stats cachedump 7 2  
ITEM copy\_test1 \[250 b; 1207795754 s]  
ITEM copy\_test \[248 b; 1207793649 s]

stats slabs  
显示各个slab的信息，包括chunk的大小、数目、使用情况等

stats items  
显示各个slab中item的数目和最老item的年龄(最后一次访问距离现在的秒数)

stats detail \[on|off|dump]  
设置或者显示详细操作记录

参数为on，打开详细操作记录  
参数为off，关闭详细操作记录  
参数为dump，显示详细操作记录(每一个键值get、set、hit、del的次数)

stats detail dump  
PREFIX copy\_test2 get 1 hit 1 set 0 del 0  
PREFIX copy\_test1 get 1 hit 1 set 0 del 0  
PREFIX cpy get 1 hit 0 set 0 del 0