Memcached::getAllKeys

(PECL memcached >= 2.0.0)

Memcached::getAllKeysGets the keys stored on all the servers

说明

public Memcached::getAllKeys ( void ) : array

Memcached::getAllKeys() queries each memcache server and retrieves an array of all keys stored on them at that point in time. This is not an atomic operation, so it isn't a truly consistent snapshot of the keys at point in time. As memcache doesn't guarantee to return all keys you also cannot assume that all keys have been returned.

参数

此函数没有参数。

返回值

Returns the keys stored on all the servers on success 或者在失败时返回 FALSE.

User Contributed Notes

harold at snel dot me 23-Apr-2019 08:59
/**
 * Get all memcached keys. Special function because getAllKeys() is broken since memcached 1.4.23. Should only be needed on php 5.6
 *
 * cleaned up version of code found on Stackoverflow.com by Maduka Jayalath
 *
 * @return array|int - all retrieved keys (or negative number on error)
 */
public function getMemcachedKeys($host = '127.0.0.1', $port = 11211)
{
    $mem = @fsockopen($host, $port);
    if ($mem === false)
    {
        return -1;
    }

    // retrieve distinct slab
    $r = @fwrite($mem, 'stats items' . chr(10));
    if ($r === false)
    {
        return -2;
    }

    $slab = [];
    while (($l = @fgets($mem, 1024)) !== false)
    {
        // finished?
        $l = trim($l);
        if ($l == 'END')
        {
            break;
        }

        $m = [];
        // <STAT items:22:evicted_nonzero 0>
        $r = preg_match('/^STAT\sitems\:(\d+)\:/', $l, $m);
        if ($r != 1)
        {
            return -3;
        }
        $a_slab = $m[1];

        if (!array_key_exists($a_slab, $slab))
        {
            $slab[$a_slab] = [];
        }
    }

    reset($slab);
    foreach ($slab as $a_slab_key => &$a_slab)
    {
        $r = @fwrite($mem, 'stats cachedump ' . $a_slab_key . ' 100' . chr(10));
        if ($r === false)
        {
            return -4;
        }

        while (($l = @fgets($mem, 1024)) !== false)
        {
            // finished?
            $l = trim($l);
            if ($l == 'END')
            {
                break;
            }

            $m = [];
            // ITEM 42 [118 b; 1354717302 s]
            $r = preg_match('/^ITEM\s([^\s]+)\s/', $l, $m);
            if ($r != 1)
            {
                return -5;
            }
            $a_key = $m[1];

            $a_slab[] = $a_key;
        }
    }

    // close the connection
    @fclose($mem);
    unset($mem);

    $keys = [];
    reset($slab);
    foreach ($slab AS &$a_slab)
    {
        reset($a_slab);
        foreach ($a_slab AS &$a_key)
        {
            $keys[] = $a_key;
        }
    }
    unset($slab);

    return $keys;
}
flaviu dot chelaru at gmail dot com 05-Feb-2017 08:58
// initiate the memcached instance
$cache = new \Memcached();
$cache->addServer('localhost', '11211');

// get all stored memcached items

$keys = $cache->getAllKeys();
$cache->getDelayed($keys);

$store = $cache->fetchAll();

// delete by regex keys

$keys = $cache->getAllKeys();
$regex = 'product_.*';
foreach($keys as $item) {
    if(preg_match('/'.$regex.'/', $item)) {
        $cache->delete($item);
    }
}
xiangku7890 at gmail dot com 24-Apr-2016 01:46
First I use the lastest memcached version 1.4.25, but unfortunately I found memcached::getAllkeys do not work with it, though i follow the others suggestion to disable Memcached::OPT_BINARY_PROTOCOL. So i try to use history versions, when i use memcached version 1.4.17, it works.
fykknd at 163 dot com 21-Dec-2015 08:11
I got this answer..
My libmemcached version is 1.0.18. php-memcached version is 2.2.0
In Libmemcached at memcache.h line 84:
 #define MAX_NUMBER_OF_SLAB_CLASSES (63 + 1)
Modify it to 201, compile again. It's ok.
主要还是版本不兼容造成的。libmemchaed里的一个bug,
memcached_return_t这个方法里的for循环,最大数是200,和上面的常量64定义不一致造成的。
Cuchac 21-Jan-2015 07:27
This command returns always FALSE when binary protocol in use (Memcached::OPT_BINARY_PROTOCOL = true). Without binary protocol it works.