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)
{
$memcache->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:should fix your problem.