Это решение работает для версий django до 1.7
Вот решение, которое я написал, чтобы сделать то, о чем вы говорите на некоторых из моих собственных проектов:
def expire_view_cache(view_name, args=[], namespace=None, key_prefix=None):
"""
This function allows you to invalidate any view-level cache.
view_name: view function you wish to invalidate or it's named url pattern
args: any arguments passed to the view function
namepace: optioal, if an application namespace is needed
key prefix: for the @cache_page decorator for the function (if any)
"""
from django.core.urlresolvers import reverse
from django.http import HttpRequest
from django.utils.cache import get_cache_key
from django.core.cache import cache
# create a fake request object
request = HttpRequest()
# Loookup the request path:
if namespace:
view_name = namespace + ":" + view_name
request.path = reverse(view_name, args=args)
# get cache key, expire if the cached item exists:
key = get_cache_key(request, key_prefix=key_prefix)
if key:
if cache.get(key):
# Delete the cache entry.
#
# Note that there is a possible race condition here, as another
# process / thread may have refreshed the cache between
# the call to cache.get() above, and the cache.set(key, None)
# below. This may lead to unexpected performance problems under
# severe load.
cache.set(key, None, 0)
return True
return False
Django управляет этими кешами запроса представления, поэтому он создает поддельный объект запроса для кэшированного представления, использует его для извлечения ключа кеша, а затем истекает его
Чтобы использовать его так, как вы говорите, попробуйте что-то вроде:
from django.db.models.signals import post_save
from blog.models import Entry
def invalidate_blog_index(sender, **kwargs):
expire_view_cache("blog")
post_save.connect(invalidate_portfolio_index, sender=Entry)
Таким образом, в основном, когда объект записи в блоге сохраняется, вызывается invalidate_blog_index и срок действия кэшированного представления истекает. NB: я не проверял это всесторонне, но пока он работал нормально для меня.