【问题标题】:Memcache - values deleted from memcache reappearingMemcache - 从 memcache 中删除的值重新出现
【发布时间】:2016-11-11 08:04:56
【问题描述】:

我正在使用 memcache 来存储 Zend_Config(和其他值) - 我将值设置如下:

$memcache = new Memcache();
...

if (!$config = $memcache->get($memcache->unique_key.APPLICATION_DOMAIN."#config"))
{
    ...
    $memcache->set($memcache->unique_key.APPLICATION_DOMAIN."#config", $config);
}

我删除的值如下:

$memcache->delete($key);

从 memcache 中删除值后,它会正确显示在与删除相同的连接中 - 调用 $memcache->get($key) 正确地给了我 NULL。但是,当我刷新脚本(并与 memcache 建立新连接)时,数据会重新弹出,就好像 memcache 状态没有更新一样。我尝试使用 replace 代替(具有一些特定值),效果相同 - 值不会更新。

调用$memcache->flush() 有效,并从内存缓存中删除所有内容,但我想删除特定键。

在手册页上有一条 5 年前关于 PECL 版本和 memcached 之间不兼容的神秘消息(但那是 5 年前的)。有人可以向我解释可能发生的事情吗?

我在 PHP 5.6 上使用 memcached 1.4.21 和 memcache (PECL) 3.0.8

【问题讨论】:

  • delete 方法应该返回 truefalse 而不是 NULL - 你如何检查它返回的值?
  • @skrilled - 我没有写到delete 方法返回NULL 而是“正确调用$memcache->get($key) 给我NULL”。 delete 实际上返回 true。
  • 哦,好吧,很抱歉造成误会。
  • 你“刷新你的脚本”意味着你让它再次运行?将变量写入内存缓存的脚本是否相同?
  • @LarsStegelitz - 我从内存缓存中的值开始。我正在运行一个从内存缓存中删除值的脚本。然后,在将任何内容放入内存缓存之前,我会使用 exit 在内存缓存中显示值。

标签: php memcached


【解决方案1】:

问题源于此的用法:

$list = array();
$allSlabs = $memcache->getExtendedStats('slabs');

foreach ($allSlabs as $server => $slabs)
    foreach ($slabs as $slabId => $slabMeta)
        foreach ($memcache->getExtendedStats('cachedump', (int) $slabId) as $entries)
            if (!empty($entries))
                foreach ($entries as $key => $entry)
                    if (strpos($key, $memcache->unique_key) === 0)
                    {
                        $value = $memcache->get($key);
                        $list[$key] = is_string($value) && unserialize($value) ? unserialize($value) : $value;
                    }

(您会注意到它没有包含在我的原始查询中,因为它不任何事情,而是列出存储在 memcached 中的键/值)

此功能的问题在于,它显然会默默地导致 memcached 变为只读。虽然在函数的后续调用中提供的值被更新(我认为它们是从本地内存中检索的),但 memcached 服务中的值不是

此行为会影响当前最新版本的 memcached - 中断行为是在 memcache (2.2.x) 中引入的

如果有人想调查这种行为,我会继续悬赏

【讨论】:

    【解决方案2】:

    对我来说非常有效的一种方法是使用相同的密钥再次设置缓存,但将到期时间放在现在之前几秒钟。这会使该密钥在您的缓存中过期。 c#代码是这样的:

    memcachedCache.Set(objectName, yourvalue, DateTime.Now.AddSeconds(-10)); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      相关资源
      最近更新 更多