4

I want to use per-view cache. I know how it's working, but where's the problem? How can I invalidate that cache? I must do it each time database records are changed. There is no info about how to do that:/

robos85
  • 2,484
  • 5
  • 32
  • 36

2 Answers2

10

This is a django snippet I found that might be helpful:

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)

Otherwise this SO question goes into more detail regarding this: Expire a view-cache in Django?

Community
  • 1
  • 1
Drekembe
  • 2,620
  • 2
  • 16
  • 13
2

Take a look at this snippet http://djangosnippets.org/snippets/936/. Pass the path(url) of the view to the expire_page function function, every time you want to invalidate the cache.

tukan
  • 71
  • 2
  • As url, should I pass a result of reverse function? I pass that: expire_page(reverse('index_show_freelinks', kwargs={'name':'partners'})) but cache is still valid. – robos85 Aug 19 '11 at 23:50
  • Are you sure that `reverse` is returning the URL you want? Maybe try just hardcoding it to see if it works. How are you testing if the cache is still valid? – Drekembe Aug 20 '11 at 09:17