如何对memcache的数据(key-value)进行遍历操作
Skylin stats命令
memcache的stats命令包括:
- stats
- stats reset
- stats malloc
- stats maps
- stats sizes
- stats slabs
- stats items
- stats cachedump slab_id limit_num
- stats detail [on|off|dump]
通过这些stats命令我们就可以完成memcache存储的内容的遍历,OK,下面我们通过telnet直接连接到memcache通过这些命令来完成相关的操作。
telnet到192.168.15.225(局域网测试机器)的memcache服务器

执行stats items命令,可以看到出现 很多的items行。

执行stats cachedump 7 0命令。这里的7表示上面图中items后面的数字,0标示显示全部的数据,如果是1就标示只显示1条。
下图为执行后的结果,item后面的字符串为key

通过上面列出的key我们就可以遍历所有的数据了,下面我们取出某一条数据,key为UserFriendsIds_4的数据。
- get UserFriendsIds_4
到这里,你也许明白了怎么去遍历memcache的数据了。
代码实现
- <?php
- $host=‘192.168.15.225′;
- $port=11211;
- $mem=new Memcache();
- $mem->connect($host,$port);
- $items=$mem->getExtendedStats (‘items’);
- $items=$items["$host:$port"]['items'];
- foreach($items as $key=>$values){
- $number=$key;;
- $str=$mem->getExtendedStats ("cachedump",$number,0);
- $line=$str["$host:$port"];
- if( is_array($line) && count($line)>0){
- foreach($line as $key=>$value){
- echo $key.‘=>’;
- print_r($mem->get($key));
- echo "\r\n";
- }
- }
- }
- ?>
Posted in memcache |
03月 7th, 2010 at 04:39
Dazzling article . Will definitely copy it to my blog.Thanks.