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.
精彩评论