Browser delay with changing content of the page in django admin (caching, python/django)

吃可爱长大的小学妹 提交于 2019-12-07 22:58:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!