How to delete lots of related keys at once

Ramon Leon ramon.leon at allresnet.com
Fri Aug 3 02:00:39 UTC 2007


> My main confusion or concern with memcache is how to go about 
> deleting a given set of keys in the cache. For instance, we 
> probably have 10-15 pages on our site that hit the "articles" 
> tables in our database. So, that right there would be 10-15 
> unique key/value pairs in memcache, assuming I store the 
> query as the key, and the result set as the value. Now add on 
> dynamic search strings, pagination, etc, and you've probably 
> got 100 unique key/value pairs in memcache, all pertaining to 
> articles.
> 
> So my question is, if we want to make sure our site is never 
> stale, then we need to invalidate all 100 of those keys if 
> someone inserts a new article. Now I'm thinking I need to do 
> a search in my keys to find all article related keys and 
> invalidate them.
> 
> Is the best way to do this then to store ANOTHER key value 
> pair which essentially stores all the article related keys as 
> its value? So that I can quickly and easily invalidate the 
> 100 other keys?
> 
> It seems like this might be what "namespaces" are for, but I 
> can't understand the example given on the memcached FAG page.
> 
> Any help and direction in this area would be much 
> appreciated, or any other good articles I should read that 
> talk about best practices, implementation techniques, etc.
> 
> thanks in advance.
> 
> Jay

If adding a new article invalidates a hundred keys, wouldn't it seem that
you're caching at the wrong granularity?  

IMHO, memcached isn't about caching result sets whole, it's about caching
individual objects.  Were I to cache a result set, it'd only cache it as a
list of matching id's for a very limited time, but I'd cache each article
individually.  

When a new article was added, I'd save it to the database and to memcached
at the same time, keeping the cache up to date and then allow the database
to do what it's good at, querying, but just for the id's that match a query.


I'd fetch the bulk of the article data individually from memcached.  With a
list of matching id's, you could use getmulti to fetch all the articles.
I'd never bother with expiring keys, I'd let them expire naturally on their
own, or get pushed out by new data.

Ramon Leon



More information about the memcached mailing list