Django - 如何使每个视图缓存无效?

时间:2011-08-19 22:15:50

标签: django django-cache

我想使用per-view cache。我知道它是如何工作的,但问题出在哪里?如何使该缓存无效?每次更改数据库记录时都必须这样做。没有关于如何做到这一点的信息:/

2 个答案:

答案 0 :(得分:5)

这是我发现可能有帮助的django片段:

from django.core.cache import cache
from django.http import HttpRequest
from django.utils.cache import get_cache_key

def expire_page(path):
    request = HttpRequest()
    request.path = path
    key = get_cache_key(request)
    if cache.has_key(key):   
        cache.delete(key)

否则,这个问题更详细地讨论了这个问题:Expire a view-cache in Django?

答案 1 :(得分:1)

请看一下这个代码段http://djangosnippets.org/snippets/936/。每次要使缓存无效时,将视图的路径(url)传递给expire_page函数函数。

相关问题