3

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

4

2 回答 2

7

这是我发现可能有用的 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)

否则这个 SO 问题会更详细地说明:Expire a view-cache in Django?

于 2011-08-19T23:37:25.670 回答
1

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

于 2011-08-19T23:25:53.173 回答