Clear view cache using @cache_page

566 views
Skip to first unread message

Dimitris R

unread,
Nov 30, 2015, 12:43:29 PM11/30/15
to Django users
Hello, 

I am using Django 1.8.2 and caching my views using the @cache_page decorator:

@cache_page(60 * 5)
def view_name(request, obj_id):
  [...]

Is there a way to clear the cached entry programmatically, prior of the cache timeout?

Thanks, 
Dimitris

Tim Graham

unread,
Nov 30, 2015, 8:31:56 PM11/30/15
to Django users
I don't know of a simple way. Here's something I wrote to achieve that on djangoproject.com (invalidating a cached blog post on save):

https://github.com/django/djangoproject.com/blob/90e92fd18543f95685c24f84dc5e1269a5acc38a/blog/models.py#L105-L116

Adding something to Django to help with this seems useful.

Dimitris R

unread,
Dec 1, 2015, 5:25:15 AM12/1/15
to Django users
Thanks for your reply Tim. 

I have tried the code provided, but it seems that the cache is not deleted. 

I am testing the code in development, which uses django.core.cache.backends.locmem.LocMemCache. I don't know if that makes any difference. 

Also, I have noticed that the path in url.path used in request = rf.get(url.path, secure=is_secure) is not an absolute path containing the host name. While searching around, some posts suggested that the path needs to include also the host name for django 1.7+. But doing that didn't change anything. 

I tried using cache.has_key(cache_key) just before calling the delete to check if a key exists, which returns False. 


Ravi Bhushan

unread,
Jul 9, 2018, 5:07:51 AM7/9/18
to Django users

Ravi Bhushan

unread,
Jul 9, 2018, 5:07:51 AM7/9/18
to Django users
plzz solve this problem


On Monday, November 30, 2015 at 11:13:29 PM UTC+5:30, Dimitris R wrote:
Reply all
Reply to author
Forward
0 new messages