开发者

memcache php doesn't expire

I'm truing to use memcache in my PHP code:

$memcache = new Memcache;
$memcache->connect('10.0.0.21', 11244) or die ("Could not connect");
$store = 10; # in seconds
$cache_key = "Counter";

$counter = $memcache->get($cache_key);
if (empty($counter))
{
    $counter = $this->getTotal();
    $result = $memcache->replace($cache_key, $counter); 
    if($result == false) 
    { 
        $m开发者_StackOverflow社区emcache->set($cache_key, $counter, 0, $store);
    }
}

echo $counter;

What happens is that value doesn't update. It staid the same for like days - much longer that 10 sec expiration time I set in the code. What am I doing wrong? My understanding that key will be removed after 10 sec but looks like it doesn't.


You only set the expiration time if the key is not already present. You need to set it in the call to replace, too:

$result = $memcache->replace($cache_key, $counter, 0, $store); 

should fix your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜