views:

42

answers:

2

I understand the need for a function like DB_Get_Cached("sql string") that hashes the SQL in order to perform a lookup in memcached for the existence of the data.

function DB_Get_Cached(string SQL)
 data = memcache_get_data(md5(SQL))
 if (!data)
   return DB_Get(SQL)
 end if
end function

What is a good method to expand this to handle data invalidation or timeout?

I'm thinking in terms of product pages in an e-commerce site, or user details in their profile.

Thanks

+2  A: 

Don't cache in the query function but cache wherever you handle the results. That might even allow you to cache postprocessed requests (rendered HTML for example).

And, as a cache key, use a meaningful name like 'page:menu' or 'user:profile:USERID'. This will allow you to delete the cache entry easily if you want to invalidate it.

ThiefMaster
+1  A: 

You can use a global version id of some sort in your key. For example, if you had this key to represent the data for product #1234:

data:products:1234

Add a version id to the key like this:

data:1:products:1234

Then, when you want to invalidate your cache, simply increment the version id. This will cause all the cache keys to change:

data:2:products:1234
Eric Petroelje