shergill
shergill

Reputation: 3788

Memcache + PHP - Why data is not expiring?

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

Upvotes: 11

Views: 5358

Answers (3)

Kevin
Kevin

Reputation: 4287

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.

Upvotes: 2

kiddailey
kiddailey

Reputation: 3664

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

Upvotes: 5

Robbie
Robbie

Reputation: 17710

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.

Upvotes: 1

Related Questions