如何禁用Django / mod_WSGI页缓存 [英] How to Disable Django / mod_WSGI Page Caching

查看:129
本文介绍了如何禁用Django / mod_WSGI页缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Django通过mod_wsgi在Apache中运行。我相信Django正在缓存我的页面服务器端,这导致一些功能无法正常工作。



我有一个倒计时器,通过获取当前的服务器时间,确定剩余倒数时间,并将该数字输出到HTML模板。一个JavaScript倒数计时器然后接管并运行该用户的倒计时。



当用户刷新页面时出现问题,或使用倒数计时器导航到其他页面。定时器似乎偶尔跳到不同的时间,通常每次刷新一次又一次地回到同一时间。



使用HTTPFox,该页面未被加载从我的浏览器缓存,所以看起来像Django或Apache缓存页面。有没有办法禁用此功能?我不会有足够的流量来担心缓存脚本输出。或者我完全错了为什么会发生这种情况?



从下面的帖子,Django看起来像缓存是禁用的,这意味着它必须在其他地方发生,可能在Apache?



对于发生了什么,我有更全面的描述:对于服务器发出的前7(或者更多)请求,页面由脚本呈现并返回,尽管这7个页面中的每一个似乎被缓存,因为它稍后显示。在第8个请求中,服务器提供第一页。在第9个请求中,它提供了第二页,等等在一个循环中。这一直持续到我重新启动apache,当进程重新开始。



我已经配置了mod_wsgi一次只运行一个进程,这导致定时器在每种情况下重置为相同的值。有趣的是,我的页面上还有一个组件,在每个请求上显示一个随机图像,使用顺序('?'),每次都会刷新不同的图像,这表明缓存正在Django而不是在Apache中发生。



根据之前的编辑,我回去并查看了相关的views.py文件,发现倒数开始变量在模块中被全局设置在视图功能之外。在视图中移动该设置功能解决了问题。所以结果是不是一个缓存问题。感谢大家对此的帮助。

解决方案

根据我在Apache中的mod_wsgi的经验,它们不太可能导致缓存。一些事情要尝试:


  1. 有可能你有一些代理服务器在您的计算机和Web服务器之间适当或不适当地缓存页面。有时ISP会运行代理服务器以减少网络外的带宽。你可以提供缓存页面的HTTP标头(Firebug可以给你这个)。我特别感兴趣的标题包括Cache-Control,Expires,Last-Modified和ETag。

  2. 可以从settings.py文件发布您的MIDDLEWARE_CLASSES。您可能有一个中间件为您执行缓存。

  3. 您可以为以下项目加载代码加载缓存,django.core.cache和cache_page 。 a * grep -Rsearch**将工作。

  4. settings.py(或从localsettings import *导入的任何内容)包含CACHE_BACKEND?

  5. 重新启动apache会怎么样? (例如sudo services apache restart)。如果重新启动会清除该问题,那么可能是apache执行缓存(这可能会清除一个locang Django缓存后端)


I have Django running in Apache via mod_wsgi. I believe Django is caching my pages server-side, which is causing some of the functionality to not work correctly.

I have a countdown timer that works by getting the current server time, determining the remaining countdown time, and outputting that number to the HTML template. A javascript countdown timer then takes over and runs the countdown for the user.

The problem arises when the user refreshes the page, or navigates to a different page with the countdown timer. The timer appears to jump around to different times sporadically, usually going back to the same time over and over again on each refresh.

Using HTTPFox, the page is not being loaded from my browser cache, so it looks like either Django or Apache is caching the page. Is there any way to disable this functionality? I'm not going to have enough traffic to worry about caching the script output. Or am I completely wrong about why this is happening?

[Edit] From the posts below, it looks like caching is disabled in Django, which means it must be happening elsewhere, perhaps in Apache?

[Edit] I have a more thorough description of what is happening: For the first 7 (or so) requests made to the server, the pages are rendered by the script and returned, although each of those 7 pages seems to be cached as it shows up later. On the 8th request, the server serves up the first page. On the 9th request, it serves up the second page, and so on in a cycle. This lasts until I restart apache, when the process starts over again.

[Edit] I have configured mod_wsgi to run only one process at a time, which causes the timer to reset to the same value in every case. Interestingly though, there's another component on my page that displays a random image on each request, using order('?'), and that does refresh with different images each time, which would indicate the caching is happening in Django and not in Apache.

[Edit] In light of the previous edit, I went back and reviewed the relevant views.py file, finding that the countdown start variable was being set globally in the module, outside of the view functions. Moving that setting inside the view functions resolved the problem. So it turned out not to be a caching issue after all. Thanks everyone for your help on this.

解决方案

From my experience with mod_wsgi in Apache, it is highly unlikely that they are causing caching. A couple of things to try:

  1. It is possible that you have some proxy server between your computer and the web server that is appropriately or inappropriately caching pages. Sometimes ISPs run proxy servers to reduce bandwidth outside their network. Can you please provide the HTTP headers for a page that is getting cached (Firebug can give these to you). Headers that I would specifically be interested in include Cache-Control, Expires, Last-Modified, and ETag.
  2. Can you post your MIDDLEWARE_CLASSES from your settings.py file. It possible that you have a Middleware that performs caching for you.
  3. Can you grep your code for the following items "load cache", "django.core.cache", and "cache_page". A *grep -R "search" ** will work.
  4. Does the settings.py (or anything it imports like "from localsettings import *") include CACHE_BACKEND?
  5. What happens when you restart apache? (e.g. sudo services apache restart). If a restart clears the issue, then it might be apache doing caching (it is possible that this could also clear out a locmen Django cache backend)

这篇关于如何禁用Django / mod_WSGI页缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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