S
S
Sasha Brahms2015-11-20 12:36:17
PHP
Sasha Brahms, 2015-11-20 12:36:17

Get everything from Memcache?

How to get everything that is in Memcache?
I don’t know all the keys, I need to get it and look ..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
De YURII, 2015-11-20
@Dejurin

/**
 * @ param string $server
 * @ param int $port
 * @ param int $limit
 * @ return array
 */
public function getMemcacheKeys ($server, $port, $limit = 10000)
{
    $keysFound = array();

    $options = $this->_options;
    $server = $options['servers'][0];
    $memcache = new Memcache;
    $memcache->connect($server, $port = 11211, 5);

    $slabs = $memcache->getExtendedStats('slabs');
    foreach ($slabs as $serverSlabs) {
        foreach ($serverSlabs as $slabId => $slabMeta) {
            try {
                $cacheDump = $memcache->getExtendedStats('cachedump', (int) $slabId, 1000);
            } catch (Exception $e) {
                continue;
            }

            if (!is_array($cacheDump)) {
                continue;
            }

            foreach ($cacheDump as $dump) {

                if (!is_array($dump)) {
                    continue;
                }

                foreach ($dump as $key => $value) {
                    $keysFound[] = $key;

                    if (count($keysFound) == $limit) {
                        return $keysFound;
                    }
                }
            }
        }
    }

    return $keysFound;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question