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

时间:2023-01-10 20:22:14

Its not to hard to invalidate a particular template cache in django

它并不难以使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)

However I have a situation where I need to delete all cached fragments that have had a certain variable passed to them. For example, delete all cached fragments about the car brand 'Toyota'.

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

{% cache 100000 car_content car.brand %}

Essentially is there a way to get all cache_keys based on a certain set of criteria? I have thought dangerously about changing the cache source but I was wondering if there might be a better solution to this problem.

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

2 个解决方案

#1


1  

I do this with caching namespaces. Here is a decent explanation:

我这样做是为了缓存命名空间。这是一个不错的解释:

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

#2


0  

Use a date in the cache key:

在缓存键中使用日期:

{% cache 100000 car_content car.brand car.brand.last_modified %}

That way all fragments are created each time the brand gets modified.

这样,每次品牌修改时都会创建所有片段。

#1


1  

I do this with caching namespaces. Here is a decent explanation:

我这样做是为了缓存命名空间。这是一个不错的解释:

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

#2


0  

Use a date in the cache key:

在缓存键中使用日期:

{% cache 100000 car_content car.brand car.brand.last_modified %}

That way all fragments are created each time the brand gets modified.

这样,每次品牌修改时都会创建所有片段。