Memcached的管理
在以前写过了"搭建nginx + python + django +memcached+ mysql +fastcgi 环境"一文,有不少朋友问我关于memcached的管理的问题,比如查看memcached的运行情况等等,我们知道目前memcached没有直观的工具来查看整个运行情况,不过我们可以通过系统级--shell命令。
1、数据存储(假设key为dbasky.net,value为88888)
printf "set dbasky.net 0 0 5\r\n88888\r\n" | nc 127.0.0.1 11211
STORED
2、数据取回(假设key为dbasky.net)
printf "get dbasky.net\r\n" | nc 127.0.0.1 11211
VALUE dbasky.net 0 5
88888
END
3、数值增加1(假设key为dbasky.net,并且value为正整数)
printf "incr dbasky.net 1\r\n" | nc 127.0.0.1 11211
88889
4、数值减少3(假设key为dbasky.net,并且value为正整数)
printf "decr dbasky.net 3\r\n" | nc 127.0.0.1 11211
88886
5、数据删除(假设key为dbasky.net)
printf "delete dbasky.net\r\n" | nc 127.0.0.1 11211
DeleteD
6、查看Memcached状态
[root@app3 ~]# printf "stats\r\n" | nc 127.0.0.1 11211
STAT pid 20272
STAT uptime 927901
STAT time 1256541017
STAT version 1.2.8
STAT pointer_size 64
STAT rusage_user 72323.142211
STAT rusage_system 270758.303481
STAT curr_items 2255461
STAT total_items 2237096141
STAT bytes 3865876133
STAT curr_connections 156
STAT total_connections 2662892830
STAT connection_structures 2975
STAT cmd_flush 0
STAT cmd_get 7155551459
STAT cmd_set 2237096141
STAT get_hits 6275571127
STAT get_misses 879980332
STAT evictions 21174550
STAT bytes_read 5936788593022
STAT bytes_written 12278494693042
STAT limit_maxbytes 4294967296
STAT threads 9
STAT accepting_conns 1
STAT listen_disabled_num 0
END
7、模拟top命令,查看Memcached状态:
watch "printf 'stats\r\n' | nc 127.0.0.1 11211"
或者
watch "echo stats | nc 127.0.0.1 11211"
[root@app3 ~]# watch "printf 'stats\r\n' | nc 127.0.0.1 11211"
Every 2.0s: printf 'stats\r\n' | nc 127.0.0.1 11211 Mon Oct 26 16:11:26 2009
STAT pid 20272
STAT uptime 927970
STAT time 1256541086
STAT version 1.2.8
STAT pointer_size 64
STAT rusage_user 72327.750510
STAT rusage_system 270774.932953
STAT curr_items 2255435
STAT total_items 2237240981
STAT bytes 3865771193
STAT curr_connections 269
STAT total_connections 2663066932
STAT connection_structures 2975
STAT cmd_flush 0
STAT cmd_get 7156031867
STAT cmd_set 2237240981
STAT get_hits 6275980308
STAT get_misses 880051559
STAT evictions 21177694
STAT bytes_read 5937174840056
STAT bytes_written 12279303680177
STAT limit_maxbytes 4294967296
STAT threads 9
STAT accepting_conns 1
STAT listen_disabled_num 0
END
下面解释下各项的含意:
uptime 是memcached运行的秒数,cmd_get是查询缓存的次数。这两个数据相除一下就能得到平均每秒请求缓存的次数,cmd_set 就是设置key=>value的次数。整个memcached是个大hash,用cmd_get没有找到的内容,就会调用一下cmd_set写进缓存里。紧跟着是get_hits,就是缓存命中的次数。缓存命中率 = get_hits/cmd_get *100%。get_misses的数字加上get_hits应该等于cmd_get。而total_itemscurr_items表示现在在缓存中的键值对个数,在图上total_items == cmd_set == get_misses,不过当可用最大内存用光时,memcached就会删掉一些内容,上面的等式就不成立了。