简体   繁体   中英

Php Memcached delete not working

I am using PHP Memcached & when I delete a key, I can still retrieve the key. What could I be doing wrong?

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"));

After running this, the get still returns a value. Not sure what is going on here.

You should also delete cache from _cache property in terms of the retrieving method:

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

But do not apply this code design in your production environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM