0

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.

user2158382
  • 4,430
  • 12
  • 55
  • 97
  • What is `$this->_cache[$key]` and how is it related to memcached? – Cheery Nov 04 '14 at 00:56
  • 1
    Gotta say it... Maybe its cached? 8) – Digitalis Nov 04 '14 at 01:03
  • Now, when more code is presented - `After running this, the get still returns a value.` because it is taken from `$this->_cache` and you are not clearing it on `delete`. Or do you mean in a new request to the script? – Cheery Nov 04 '14 at 01:23

1 Answers1

1

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.

grant sun
  • 120
  • 5