http://iamcaihuafeng.blog.sohu.com/159082880.html
1.方法一
telnet memcached上面以后用命令方式操作
stats items
STAT items:1:number 3
STAT items:1:age 800
END
items:后面的数字表示memcached的slab id
stats cachedump 1 0
ITEM data2 [19 b; 1283239476 s]
ITEM data3 [6 b; 1283239476 s]
ITEM data4 [20 b; 1283239476 s]
END
2.方法二
用php进行操作
<?php
$host = 'localhost';
$port = 11211;
$mem = new Memcache();
$mem -> connect($host,$port);
$items = $mem -> getExtendedStats('items');
print_r($items);
$items = $items["$host:$port"]['items'];
print_r($items);
foreach ($items as $key => $values){
echo "key: $key<br />/n";
$number = $key;
$str = $mem -> getExtendedStats("cachedump", $number, 0);
print_r($str);
$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";
}
}
}
?>
输出如下:
Array
(
[localhost:11211] => Array
(
[items] => Array
(
[1] => Array
(
[number] => 3
[age] => 19
)
)
)
)
Array
(
[1] => Array
(
[number] => 3
[age] => 19
)
)
key: 1<br />
Array
(
[localhost:11211] => Array
(
[data4] => Array
(
[0] => 20
[1] => 1283239361
)
[data3] => Array
(
[0] => 6
[1] => 1283239361
)
[data2] => Array
(
[0] => 19
[1] => 1283239361
)
)
)
data4=>["notebook","stock"]
data3=>"news"
data2=>["disk","favorite"]
3.总结
遍历memcached中的item有什么作用呢?我个人认为大概有如下的作用:
a.可以知道memcached中保存了一些什么样的数据
b.如果想要对memcached中的数据进行批量操作(比如批量删除一些数据、给部分数据增加或减少失效时间等等),如果不能遍历,还真不行。