Dumping MemcacheD Content (Keys) with PHP

MemcacheD is a very cool piece of software. When i did some optimization of a cluster based webapp lately, i was wondering how Memcache was speading my cache entries over the cluster. So i did some research for monitoring tools. A simple approach to monitoring would be to use Cacti to monitor status values like Cache usage, hits/sec etc. This can be done by a template like this. An alternative was provided by Harun Yayli: His memcache.php which somewhat resembles the APC status page and now is a part of PECL/memcache is easy to setup and works well. It also allows to dive into the data structures but you need to decide for one server first, then click into a "Slab" and then you see the keys. Inspired by this, i wrote a small script that fetches all data from a memcache cluster, gets all keys out of it and then sorts and displays them in a list. Yes it it ugly and yes: the memcacheD is not answering other requests while doing a cachedump. But i found it very useful. Maybe somebody has something more advanced that he wants to point me to? This could save a lot of time...

But here is my naive approach: Assumed you have a memcache object in PHP 5 with some servers registered using addServer Method, the following can be used:

	    $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 $server => $entries) {
if($entries) {
foreach($entries AS $eName => $eData) {
$list[$eName] = array(
'key' => $eName,
'server' => $server,
'slabId' => $slabId,
'detail' => $eData,
'age' => $items[$server]['items'][$slabId]['age'],
);
}
}
}
}
}
ksort($list);

I then added a very simple HTML Table output to list the result array and found out some interesting details about my application that i dont want to share here ;-)

It worked for me but it could be that for bigger Memcache Clusters (mine has only 10 servers) or big content tables this will not return the complete content. More to come...

Auf Facebook teilen

« Wo er recht hat - days in the monastery »