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)
da开发者_如何学编程ta = 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
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
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.
精彩评论