问题
I have a bit weird problem witch caching on my project in django.
I can edit my page-content in django-admin. When i do that and refresh site - nothing is happening. I have to wait few minutes for changes. Funny thing is that, when i change browser (or computer) - i dont have to wait - the changes are on. Is it the problem of django, browser or what? Is it possible to set setting.py to get changes immediately?
By the way, i have already figured out that when i turn the "django.middleware.cache.FetchFromCacheMiddleware" off - the problem disapears, but i dont want to turn cache off...
Any ideas?
回答1:
Yes. If you want to keep your site-wide cache on but you want to make sure that the cache gets cleared whenever your content is updated or added, you can implement a django signals to detect the add/update/delete event and clear the cache.
Django signals - https://docs.djangoproject.com/en/dev/ref/signals/
Here's a code snippet example:-
from django.db.models.signals import post_save
@receiver(post_save, sender=BlogPost)
def clear_cache(sender, instance, created, **kwargs):
if instance.published_on is not None:
cache.delete('feed')
In this example, whenever the BlogPost model is "saved" (added or updated), the feed
key in the cache will be deleted. In your case, you will have to implement page-content
(something like this cache.delete('page-content')
and decide which corresponding model will be your sender
that triggers the clearing of the cache when it is being saved.
来源:https://stackoverflow.com/questions/11692538/browser-delay-with-changing-content-of-the-page-in-django-admin-caching-python