PHP实现查询Memcache内存中的所有键与值
使用Memcache时,我们可以用memcache提供的get方法,通过键查询到当前的数据,但是有时候需要查询内存中所有的键和值,这个时候可以使用下面的代码实现:
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: Steven 5 * Date: 2016/9/22 6 * Time: 17:21 7 */ 8 9 $host = '127.0.0.1'; 10 $port = 11211; 11 $mem = new Memcache(); 12 $mem->connect($host, $port); 13 $items = $mem->getExtendedStats('items'); 14 $items = $items["$host:$port"]['items']; 15 foreach ($items as $key => $values) { 16 $number = $key;; 17 $str = $mem->getExtendedStats("cachedump", $number, 0); 18 $line = $str["$host:$port"]; 19 if (is_array($line) && count($line) > 0) { 20 echo "<table border=2>"; 21 echo "<tr><th>键</th><th>值</th></tr>"; 22 foreach ($line as $key => $value) { 23 echo "<tr>"; 24 echo "<td style='width:30%'>"; 25 echo $key; 26 echo "</td>"; 27 echo "<td style='width: 70%'>"; 28 var_dump($mem->get($key)); 29 echo "</td>"; 30 echo "</tr>"; 31 } 32 echo "</table>"; 33 } 34 }
显示效果: