乳房太大必要时要缩小_美美生活馆

如何对memcache的数据(key-value)进行遍历操作

01月 23rd, 2010 by Skylin

stats命令

memcache的stats命令包括:

 
  1.         stats    
  2.   
  3.         stats reset    
  4.   
  5.         stats malloc    
  6.   
  7.         stats maps    
  8.   
  9.         stats sizes    
  10.   
  11.         stats slabs    
  12.   
  13.         stats items    
  14.   
  15.         stats cachedump slab_id limit_num    
  16.   
  17.         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的数据。

 
  1. get UserFriendsIds_4  

到这里,你也许明白了怎么去遍历memcache的数据了。

 

代码实现

 
  1. <?php  
  2.   
  3.         $host=‘192.168.15.225′;  
  4.   
  5.         $port=11211;  
  6.   
  7.         $mem=new Memcache();  
  8.   
  9.         $mem->connect($host,$port);  
  10.   
  11.         $items=$mem->getExtendedStats (‘items’);  
  12.   
  13.         $items=$items["$host:$port"]['items'];  
  14.   
  15.         foreach($items as $key=>$values){  
  16.   
  17.             $number=$key;;  
  18.   
  19.          $str=$mem->getExtendedStats ("cachedump",$number,0);  
  20.   
  21.          $line=$str["$host:$port"];  
  22.   
  23.          ifis_array($line) && count($line)>0){  
  24.   
  25.              foreach($line as $key=>$value){  
  26.   
  27.                  echo $key.‘=>’;  
  28.   
  29.                  print_r($mem->get($key));  
  30.   
  31.                  echo "\r\n";  
  32.   
  33.              }  
  34.   
  35.          }  
  36.   
  37.      }  
  38.   
  39.      ?>  

Posted in memcache |

One Response

  1. Рекламный Брокер Says:

    Dazzling article . Will definitely copy it to my blog.Thanks.

Leave a Comment