2014-11-04 67 views
0

我使用PHP Memcached &当我删除一个密钥时,我仍然可以检索密钥。我可能做错了什么?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仍返回一个值。不知道这里发生了什么。

+0

什么是'$ this - > _cache [$ key]',它与memcached有什么关系? – Cheery 2014-11-04 00:56:21

+1

要说......也许它的缓存? 8) – Digitalis 2014-11-04 01:03:53

+0

现在,当提供更多代码时:'运行此代码后,get仍返回一个值,因为它取自'$ this - > _ cache',并且您没有在delete中清除它。或者你的意思是在对剧本的新要求中? – Cheery 2014-11-04 01:23:29

回答

1

你也应该在检索方法上从_cache属性删除缓存:

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

但是,不要在生产环境中使用这个代码的设计。

+0

这可行,但为什么我不想在生产中应用此代码设计? – user2158382 2014-11-04 01:37:00

+0

也是如何来'unset(self :: instance() - > _cache [$ key]);'工作,但是'unset($ current_cache [$ key]);'不起作用? – user2158382 2014-11-04 01:41:36