Bre*_*den 8 python django memcached django-cache
我正在为django项目使用视图缓存.
它说缓存使用URL作为密钥,所以我想知道如果用户更新/删除对象,如何清除其中一个密钥的缓存.
例如:用户将博客帖子发布到domain.com/post/1234/..如果用户编辑了该文章,我想通过在保存编辑后的帖子的视图末尾添加某种删除缓存命令来删除该URL的缓存版本.
我正在使用:
@cache_page(60 * 60)
def post_page(....):
Run Code Online (Sandbox Code Playgroud)
如果post.id是1234,似乎这可能会起作用,但它不是:
def edit_post(....):
# stuff that saves the edits
cache.delete('/post/%s/' % post.id)
return Http.....
Run Code Online (Sandbox Code Playgroud)
jul*_*ria 18
从django缓存文档中,它说cache.delete('key')应该足够了.所以,我想到了你可能遇到的两个问题:
您的导入不正确,请记住您必须cache从django.core.cache模块导入:
from django.core.cache import cache
# ...
cache.delete('my_url')
Run Code Online (Sandbox Code Playgroud)您使用的密钥不正确(可能使用完整的URL,包括"domain.com").要检查哪个是确切的URL,您可以进入shell:
$ ./manage.py shell
>>> from django.core.cache import cache
>>> cache.has_key('/post/1234/')
# this will return True or False, whether the key was found or not
# if False, keep trying until you find the correct key ...
>>> cache.has_key('domain.com/post/1234/') # including domain.com ?
>>> cache.has_key('www.domain.com/post/1234/') # including www.domain.com ?
>>> cache.has_key('/post/1234') # without the trailing / ?
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
19395 次 |
| 最近记录: |