phpmemcached

How can I query memcached with php to get a list of all its keys in storage?


I need to replicate memcached to another key value system (couchbase). How can I query the contents of a memcached server to get a list of what is in there so that I can copy it over?


Solution

  • memcache >= 2.0.0

    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>';
                    }
                }
            }
        }   
    }