I’m using Memcached in conjunction with my PHP web app written in CodeIgniter. I utilize this Memcached library https://github.com/tomschlick/memcached-library When I cache data, I give it an expiration time of 7200, or 2 hours.
Snippet of a model query:
$result = $this->memcached_library->get(md5($sql));
if (!$result) {
$cursor = $this->db->query($sql);
$result = $cursor->row();
$this->memcached_library->set(md5($sql), $result, 7200);
}
return $result;
Cool, this works for setting data into Memcached. I can see the results, all is working well. The problem comes in that after 2 hours after placing this data into Memcached.
From my understanding, when executing the get function, Memcached should recognize that the cached data has hit past its expired date and thus marks it as invalid (not necessarily deleting it from memory however). When the PHP call to get the data, it should return false, which will then in turn cause my if statement to be evaluated as true and refetch the data and set the data in Memcached yet again.
However, it seems as if Memcached never says the data is invalid, and the same old data is in there from before the 2 hour expiration limit. If I manually call flush on Memcached (invalidating all data in the cache), the data is set into Memcached properly yet again, but we run into the same 2 hour expiration limit problem yet again.
From memcached manual:
So this should be NULL after expiration time. If it happens otherwise you have found a bug in Memcached or this library. Are you sure you are using “Memcached” and not “Memcache”. From sources of the lib you linked:
Also i see this library is using local cache that is ignoring the expiry date:
So if your php script is running as daemon for a long period of time no actual Memcached request will be sent.