0

我正在使用 PHP Memcached & 当我删除一个键时,我仍然可以检索该键。我可能做错了什么?

function __construct() {
    $this->_cache = array();

    // if we have memcache support, load it from CACHE_POOL
    //
    if (class_exists('Memcached')) {
        $this->_mc = new Memcached('CACHE_POOL');
        $servers = $this->_mc->getServerList();
        if (empty($servers)) {
            //This code block will only execute if we are setting up a new EG(persistent_list) entry
            $this->_mc->setOption(Memcached::OPT_RECV_TIMEOUT, 1000);
            $this->_mc->setOption(Memcached::OPT_SEND_TIMEOUT, 3000);
            $this->_mc->setOption(Memcached::OPT_TCP_NODELAY, true);
            $this->_mc->setOption(Memcached::OPT_PREFIX_KEY, "md_");
            $this->_mc->addServers(self::$_MEMCACHE_IPS);
        }

        $current_cache = $this->_mc->get(self::CACHE_KEY);

        if ($current_cache) {
            $this->_cache = array_merge($this->_cache, $current_cache);
        }
    }

}

    function delete($key) {
        self::instance()->_mc->delete($key);
    }

    function getSafe($key) {
        return isset($this->_cache[$key]) ? $this->_cache[$key] : FALSE;
    }

self::instance()->delete("test");
echo(self::instance()->getSafe("test"));

运行后,get 仍然返回一个值。不知道这里发生了什么。

4

1 回答 1

1

您还应该根据检索方法从 _cache 属性中删除缓存:

function delete($key) {
    self::instance()->_mc->delete($key);
    unset(self::instance()->_cache[$key]);
}

但不要在您的生产环境中应用此代码设计。

于 2014-11-04T01:30:39.637 回答