I have a simple example where I set a value for 5 seconds. The problem is that after 5 seconds; I still get back a value when I expected 'false'.
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$memcache->set('foo', 'bar', 0, 5); // 5 seconds expiry
var_dump($memcache->get('foo')); // bar
sleep(10);
var_dump($memcache->get('foo')); // still shows bar
Here is the memcache server version
Server's version: 1.4.13
When memcached needs to store new data in memory, and the memory is already full, what happen is this: memcached searches for a a suitable* expired entry, and if one is found, it replaces the data in that entry.
How does Memcached work? Unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds.
memcached can process over 50 million keys per second on a 48 core machine using only RAM and heavy batching.
Late to the game, but in your code it looks like you are passing "0" for the expiration (not "5"), which translates to "never expire" Specifically:
$memcache->set('foo', 'bar', 0, 5); // 5 seconds expiry
Should be:
$memcache->set('foo', 'bar', 5); // 5 seconds expiry
Unless I'm misunderstanding the PHP documentation located here, which shows that the set command takes three parameters:
public bool Memcached::set ( string $key , mixed $value [, int $expiration ] )
Edit: Whoops, I see that you're using the Memcache extension and not Memcached, which does have four parmaters. Perhaps try using the MEMCACHE_COMPRESSED constant instead of 0 to see if it works:
$memcache->set('foo', 'bar', MEMCACHE_COMPRESSED, 5); // 5 seconds expiry
There was a bug in the memcached server including version 1.4.13 through at least 1.4.14 that, once the memcached server had been running for some time, it would sometimes get into a mode where it would fail to properly expire values.
Restarting the service fixed it for me, and I'm hopeful that the newer versions fix it more permanently.
As the code looks fine - the next chain down the line is to look at either your version of the memcache PHP extension not working, or memcached server itself.
This get a but tricky. Easiest is to rule out memcached server first. (There's a php interface you can install - but that won't help you work outwhich bit.) So...
In terminal (or command window in Windows) type
telnet localhost 11211
(Note - telnet client is not installed in Windows by default - go to "control panel", "turn windows features on or off" and add from there.)
This gives you access to memcached.
Then type
stats items
which lists the memory items in memcached.
Hopefully you've only got one slab, so note its number and type
stats cachedump [Number] 0
And this will list what's recorded in cache.
If this still shows "bar" (in encoded format) then it's memcached server that's not working - upgrade for a newer version.
If this doesn't show "bar" (or, preferably, the item just doesn't exist - you get ERROR instead) then it'll be the memcache extension to PHP that's not working. Again, check your version of that.
When done, type
quit
Alternative is to check out "memcached" (php extension) and rewrite your PHP code with those classes. It's newer. If that still fails, it's definately memcached server; if that works that it was the php memcache extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With