Memcache::getExtendedStats

(PECL memcache >= 2.0.0)

Memcache::getExtendedStats缓存服务器池中所有服务器统计信息

说明

array Memcache::getExtendedStats ([ string $type [, int $slabid [, int $limit = 100 ]]] )

Memcache::getExtendedStats() 返回一个二维关联数据的服务器统计信息。数组的key由host:port方式 组成,无效的服务器返回的统计信息被设置为false,同样的,你可以使用函数memcache_get_extended_stats()

Note:

这个函数在Memcache2.0.0版本加入。

Note:

(译注)获取Memcache内所有数据方法:首先使用getExtendedStats('slabs')获取到每个服务器上活动slabs分块的id, 然后 使用getExtendedStats('cachedump', $slabid, $limit)来获取每个slab里面缓存的项,其中$slabid是slab分块id, $limit指 期望获取其中的多少条记录。

参数

type

期望抓取的统计信息类型,可以使用的值有{reset, malloc, maps, cachedump, slabs, items, sizes}。 通过memcached协议指定这些附加参数是为了方便memcache开发者(检查其中的变动)。

slabid

用于与参数type联合从指定slab分块拷贝数据,cachedump命令会完全占用服务器通常用于 比较严格的调试。

limit

用于和参数type联合来设置cachedump时从服务端获取的实体条数。

返回值

返回一个二维关联数组的服务器统计信息或者在失败时返回FALSE

范例

Example #1 Memcache::getExtendedStats() 示例

<?php
    $memcache_obj 
= new Memcache;
    
$memcache_obj->addServer('memcache_host'11211);
    
$memcache_obj->addServer('failed_host'11211);
    
    
$stats $memcache_obj->getExtendedStats();
    
print_r($stats);
?>

以上例程会输出:

Array
(
    [memcache_host:11211] => Array
        (
            [pid] => 3756
            [uptime] => 603011
            [time] => 1133810435
            [version] => 1.1.12
            [rusage_user] => 0.451931
            [rusage_system] => 0.634903
            [curr_items] => 2483
            [total_items] => 3079
            [bytes] => 2718136
            [curr_connections] => 2
            [total_connections] => 807
            [connection_structures] => 13
            [cmd_get] => 9748
            [cmd_set] => 3096
            [get_hits] => 5976
            [get_misses] => 3772
            [bytes_read] => 3448968
            [bytes_written] => 2318883
            [limit_maxbytes] => 33554432
        )

    [failed_host:11211] => false
)

参见

User Contributed Notes

jcastromail at yahoo dot es 23-Feb-2017 05:39
" The cachedump stat type has been removed from the memcached daemon due to security reasons. "

To the date, the version 1.4.5_4_gaa7839e (windows 64bits) still supports the command cachedump that its highly important to returns the keys stored.
eithed at gmail 29-Jul-2016 12:14
In response to manmca dot 2280 at gmail dot com

This function makes the memcached read only, at least with the most recent version of PECL memcache (3.0.8) and most recent version of memcache (1.4.21), so if you're relying on this to overwrite / remove only certain keys you're in for a nasty suprise
oushunbao at 163 dot com 16-Jun-2011 11:57
Get lists of all the keys stored in memcache server....

<?php
/**
 * Function to get all memcache keys
 * @author Manish Patel
 * @Created:  28-May-2010
 * @modified: 16-Jun-2011
 */
function getMemcacheKeys() {

   
$memcache = new Memcache;
   
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect to memcache server");

   
$list = array();
   
$allSlabs = $memcache->getExtendedStats('slabs');
   
$items = $memcache->getExtendedStats('items');
    foreach(
$allSlabs as $server => $slabs) {
        foreach(
$slabs AS $slabId => $slabMeta) {
          
$cdump = $memcache->getExtendedStats('cachedump',(int)$slabId);
            foreach(
$cdump AS $keys => $arrVal) {
                if (!
is_array($arrVal)) continue;
                foreach(
$arrVal AS $k => $v) {                  
                    echo
$k .'<br>';
                }
           }
        }
    }  
}
//EO getMemcacheKeys()
?>

copy from up, but fix a warning
i only add one line:  if (!is_array($arrVal)) continue;
manmca dot 2280 at gmail dot com 29-May-2010 01:35
Get lists of all the keys stored in memcache server....

<?php
/**
 * Function to get all memcache keys
 * @author Manish Patel
 * @Created:  28-May-2010 
 */
function getMemcacheKeys() {

   
$memcache = new Memcache;
   
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect to memcache server");

   
$list = array();
   
$allSlabs = $memcache->getExtendedStats('slabs');
   
$items = $memcache->getExtendedStats('items');
    foreach(
$allSlabs as $server => $slabs) {
        foreach(
$slabs AS $slabId => $slabMeta) {
          
$cdump = $memcache->getExtendedStats('cachedump',(int)$slabId);
            foreach(
$cdump AS $keys => $arrVal) {
                foreach(
$arrVal AS $k => $v) {                   
                    echo
$k .'<br>';
                }
           }
        }
    }   
}
//EO getMemcacheKeys()
?>

Hope it helps....