浏览器延迟与django admin中的页面内容不同(缓存,python / django) [英] Browser delay with changing content of the page in django admin (caching, python/django)

查看:721
本文介绍了浏览器延迟与django admin中的页面内容不同(缓存,python / django)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django的项目中有一些奇怪的问题,女巫缓存。



我可以在django-admin中编辑我的页面内容。当我这样做和刷新网站 - 没有发生任何事情。我必须等待几分钟的更改。有趣的是,当我更改浏览器(或电脑)时 - 我不必等待 - 更改已经开启。是django,浏览器还是什么问题?可以设置set.py以立即获取更改吗?



顺便说一句,我已经弄清楚,当我把
django.middleware.cache.FetchFromCacheMiddleware关闭 - 问题消失了,但我不想关闭缓存...



任何想法?

解决方案

是的。如果要保持网站整体的缓存,但是要确保缓存在更新或添加内容时清除,您可以实现django信号来检测添加/更新/删除事件并清除缓存。



Django信号 - https: //docs.djangoproject.com/en/dev/ref/signals/



以下是代码段示例: -

  from django.db.models.signals import post_save 

@receiver(post_save,sender = BlogPost)
def clear_cache发件人,实例,创建,** kwargs):
如果instance.published_on不是无:
cache.delete('feed')

在此示例中,每当BlogPost模型保存(添加或更新)时,$ code> feed 缓存将被删除。在你的情况下,你必须实现页面内容(这样的东西 cache.delete('page-content')并确定哪个对应的模型将是发件人,在保存时触发清除缓存。


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?

解决方案

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.

这篇关于浏览器延迟与django admin中的页面内容不同(缓存,python / django)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆