I'm trying to purge cache for one specific Entry when it is saved using signals.
I'm using decorators (signals
and render_to
) from 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)
- The problem is that when I save the model, I get output
"notfound key"
, so the cache continues without purge request.path
are point properly to my entry path.
Some settings:
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'
},
}
And the view:
@cache_page(60 * 60)
@render_to('artigo.html')
def artigo(request, categoria_slug, extra_slug="", artigo_slug=""):
...
Thank you.
EDIT:
I did Ilvar changes and now i'm getting "found key"
as return but I still can't delete cache:
key = _generate_cache_header_key(key_prefix=settings.CACHE_MIDDLEWARE_KEY_PREFIX, request=request)
key = key.replace(settings.LANGUAGE_CODE, settings.LANGUAGES[0][0])
Conf:
LANGUAGE_CODE = 'pt-BR'
LANGUAGES = (
('pt-BR','Portugues'),
)
- I only have the content updated when I restart the Gevent Server.