当使用信号保存一个特定条目时,我正在尝试清除它的缓存。
我正在使用来自的装饰器(signals
和render_to
)django-annoying
@signals.post_save(sender=Artigo)
def artigo_post_save(instance, **kwargs):
from django.http import HttpRequest
from django.utils.cache import get_cache_key
from django.core.cache import cache
# cache.delete(instance.get_absolute_url()) # not work
request = HttpRequest()
request.method = "GET"
request.path = '/' + instance.get_absolute_url()
print 'request path: ', request.path
key = get_cache_key(request=request,
key_prefix=settings.CACHE_MIDDLEWARE_KEY_PREFIX)
print "found key" if cache.has_key(key) else "notfound key"
if cache.has_key(key):
cache.delete(key)
cache.set(key, None, 0)
- 问题是当我保存模型时,我得到了 output
"notfound key"
,所以缓存继续没有清除 request.path
正确指向我的进入路径。
一些设置:
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
CACHE_MIDDLEWARE_KEY_PREFIX = 'cache'
CACHE_MIDDLEWARE_SECONDS = 600
CACHES = {
'default': {
'LOCATION': '',
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
},
}
和观点:
@cache_page(60 * 60)
@render_to('artigo.html')
def artigo(request, categoria_slug, extra_slug="", artigo_slug=""):
...
谢谢你。
编辑:
我做了Ilvar更改,现在我得到"found key"
了回报,但我仍然无法删除缓存:
key = _generate_cache_header_key(key_prefix=settings.CACHE_MIDDLEWARE_KEY_PREFIX, request=request)
key = key.replace(settings.LANGUAGE_CODE, settings.LANGUAGES[0][0])
会议:
LANGUAGE_CODE = 'pt-BR'
LANGUAGES = (
('pt-BR','Portugues'),
)
- 我只有在重新启动 Gevent 服务器时才更新内容。