django默认缓存是多大,清除Django中的特定缓存

I am using view caching for a django project.

It says the cache uses the URL as the key, so I'm wondering how to clear the cache of one of the keys if a user updates/deletes the object.

An example: A user posts a blog post to domain.com/post/1234/ .. If the user edits that, i'd like to delete the cached version of that URL by adding some kind of delete cache command at the end of the view that saves the edited post.

I'm using:

@cache_page(60 * 60)

def post_page(....):

If the post.id is 1234, it seems like this might work, but it's not:

def edit_post(....):

# stuff that saves the edits

cache.delete('/post/%s/' % post.id)

return Http.....

解决方案

From django cache docs, it says that cache.delete('key') should be enough. So, it comes to my mind two problems you might have:

Your imports are not correct, remember that you have to import cache from the django.core.cache module:

from django.core.cache import cache

# ...

cache.delete('my_url')

The key you're using is not correct (maybe it uses the full url, including "domain.com"). To check which is the exact url you can go into your 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 / ?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值