Stephen Woodbridge woodbri at swoodbridge.com
Mon May 29 01:24:28 UTC 2006

Joshua Thijssen wrote:
> When I add an item to the memcache and use an expiry-timestamp, it will
> expire, no matter how many times the key is read in the remaing time. Is
> it possible to create an item with a expiry-timestamp (an relative
> value, say 60seconds), which gets reset each time the item is read so
> so the item would stay in the cache until it's not read for a long time
> or when we remove it.

The concept of expiry is not obvious, nor is the behavior of the cache 
relative to LRU to newbies like myself until I asked a lot of questions. 
So I'll explain my understanding of it which might help. Experts, if I 
get this wrong please correct me, and it would help if something like 
this were written in the docs.

The expiry time does NOT mean the item will remain in the cache until 
that time, it ONLY means that if it is still in the cache after that 
time and you try to access it, it will not be returned. What? you say!

Exactly my first thoughts. The cache works by putting stuff in it and if 
  you fill it up and try to put something else in it, it then uses the 
LRU (Least Recently Used) algorithm to find an item(s) to purge to make 
room for the new item. So what this means is that if you have a small 
cache, you can turnover all the items in the cache in a small time frame 
depending on the item insert rates. Memcached is not a storage medium, 
don't expect to find an item in the cache. If you expect items to be in 
the cache for some period of time then you need to make sure you size 
your cache to be big enough that items are not purged. As I understand 
it you want to look at the cache hit/miss ratio stats and make sure that 
it is within acceptable numbers. A miss happens when you look for an 
item and it is not there.

So for your case, do not set an expiry time and the most active items 
will be in the cache assuming that it is sized large enough. The expiry 
is good if you have volatile data that has a know validity time frame. 
In general, your application needs to be well behaved if it does not 
find an item it is looking for, typically it needs to know how to 
regenerate the item in question.


More information about the memcached mailing list