Django - 基于变量使django模板缓存过期

Wer*_*rda 5 django caching django-templates

它并不难以使django中的特定模板缓存无效

def invalidate_cache_key(fragment_name, *variables):
   args = md5_constructor(u':'.join([urlquote(var) for var in variables]))
   cache_key = 'template.cache.%s.%s' % (fragment_name, args.hexdigest())
   cache.delete(cache_key)
Run Code Online (Sandbox Code Playgroud)

但是,我有一种情况需要删除所有已传递给它们的变量的缓存片段.例如,删除所有关于汽车品牌"丰田"的缓存片段.

{% cache 100000 car_content car.brand %}
Run Code Online (Sandbox Code Playgroud)

基本上有一种方法可以根据一组标准获取所有cache_keys吗?我一直在想改变缓存源,但我想知道是否有更好的解决方案可以解决这个问题.

Tyl*_*ler 1

我通过缓存命名空间来做到这一点。这是一个不错的解释:

http://blog.dberg.org/2008/07/user-based-memcached-namespaces.html