Caching collections of objects

Mike Marrone mike.marrone at cambrianhouse.com
Sat May 26 21:37:15 UTC 2007


I have recently been looking into integrating memcache into our apps, 
and had a few newbish implementation questions...
(please forgive me for any ignorance I may have towards memcache, I am 
still new to it!)

Memcache is great for retrieving/updating single objects from the cache. 
Obviously where the biggest gains are going to come from though are with 
searches and caching of collections of objects. I am debating over two 
approaches here:
1. Caching of collections (eg: "give me all the user comments related to 
foo")
2. Caching entire page outputs based upon the unique url (eg: "give me 
the xhtml output for foo/bar?baz=1")
3. A combination of #1 & #2

Getting the data into the cache and retrieving it when appropriate is a 
simple matter. It is when the data in the cache has become stale and I 
need to flush it from the cache that I become stuck as to how best to 
solve the problem.
Take for example a news article. It may have a user comments, a view 
count, user votes, etc. Furthermore the news article may appear in lists 
on separate pages under the author's profile, or in a search, etc. When 
it becomes stale, I would need to either flush all cached collections 
containing that object, or flush all cached page data where that object 
appears.

One solution for approach #2 above is to simply flush all cached page 
data whenever there are writes to the database. Though this is 
sub-optimal and would result in low cache hit rates I'm assuming.

I'm sure others out there have solved this problem already. Would anyone 
mind sharing their thoughts with me?

Thanks in advance
Mike M



More information about the memcached mailing list