开发者

Django / general caching question

开发者 https://www.devze.com 2023-04-05 18:35 出处:网络
One common pattern when caching with Django is to use the current site\'s ID in each cache key, in order to, in essence, namespace your keys. The problem I have is that I\'d love to be able to delete

One common pattern when caching with Django is to use the current site's ID in each cache key, in order to, in essence, namespace your keys. The problem I have is that I'd love to be able to delete all values in cache under a namespace (e.g. Delete all cache values for site 45 because they've made some fundamental change). The current pattern to dealing with this send signals all over the place, etc. I've used the Site.id cache key example because that is a common pattern that others may recognize, but the way I'm using cache for a custom multi-tenant application makes this problem even more profound, so my question: Is there a ca开发者_开发技巧che back-end and/or pattern that works well for deleting objects in a namespaced way, or pseudo-namespaced way, that is not extraordinarily expensive (ie, not looping through all possible cache keys for a given namespace, deleting the cache for each)? I would prefer to use memecached, but am open to any solution that works well, plug-in or not.


It's generally difficult to delete large categories of keys. A better approach is for each site to have a generation number associated with it. Start the generation at 1. Use the generation number in the cache keys for that site. When you make a fundamental change, or any other time you want to invalidate the entire cache for the site, increment the site's generation number. Now all cache accesses will be misses, until everything is cached anew. All the old data will still be in the cache, but it will be discarded as it ages, and it isn't accessed any more.

This scheme is extremely efficient, since it doesn't require finding or touching all the old data at all. It can also be generalized to any class of cache contents, it doesn't have to be per-site.


I have a feeling that Django's Cache Versioning support, new in Django 1.3.

It allows you to set a system-wide variable VERSION on your cache, or you can explicitly set it when creating a record:

cache.set("mykey", True, version=2)

With this method, when you need to update your cache, simply up your VERSION and you're in the clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消