Reputation: 58903
suppose that i cache slightly different sets of info for similar data, for example, the last 10 post titles for a user, his last 100 and his last 200.
I have something like this:
cache.set('posts' + user_id + num_posts, DATA, 30)
so, supposing that the user id is 123 I end up having cached posts12310
(for 10 posts), posts12320
(for 20 posts) etc
I get num_posts from querystring.
When a user adds a new post, I want to invalidate all the cached queries related to him, for example all caches whose key starts with posts123
. How to do so?
Also, please, mind that this is a simplification of my real problem that involves large data and not blog post titles, but the core of the problem is the same: how to group cache items so that one can invalidate a whole group. Also, I'm not using decorators but I need to make use of the lower level API.
Thanks
Upvotes: 1
Views: 59
Reputation: 31653
Do not store many subsets of the same data in the cache. Store one value with the maximum number of posts you need (i.e 200) and than after you retrieve the data from cache do list slice to get the number of results you need:
DATA = [<200 last posts here>]
cache.set('posts' + user_id, DATA, 30) # Store everything
These way you'll have just one key so it'll be easy to invalidate the cache and also you'll store less data in you cache.
# Get all cached posts and slice 10 first items
cache.get('posts' + user_id)[0:10]
# Get all cached posts and slice 100 first items
cache.get('posts' + user_id)[0:100]
Remember that you'll have to handle cases when there are less than requested values in the cache)
Upvotes: 2