有时候在使用缓存的时候,可能需要获取memcache中缓存的总数或key列表!
/** * 返回key列表 * @return type */ function getMemcacheKeys() { $memcache = new Memcache; $memcache->connect('127.0.0.1', 11211) or die("Could not connect to memcache server"); $list = array(); $i = 0; $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) { $list[$i] = $k; $i++; } } } } return $list; }
关键字词: