apache 如何禁用 Django / mod_WSGI 页面缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1633684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Disable Django / mod_WSGI Page Caching
提问by Travis
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.
我通过 mod_wsgi 在 Apache 中运行 Django。我相信 Django 正在服务器端缓存我的页面,这导致某些功能无法正常工作。
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.
我有一个倒数计时器,它通过获取当前服务器时间、确定剩余倒计时时间并将该数字输出到 HTML 模板来工作。一个 javascript 倒数计时器然后接管并为用户运行倒计时。
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?
使用 HTTPFox,该页面不会从我的浏览器缓存中加载,因此看起来 Django 或 Apache 正在缓存该页面。有什么办法可以禁用这个功能吗?我不会有足够的流量来担心缓存脚本输出。或者我完全错了为什么会这样?
[Edit] From the posts below, it looks like caching is disabled in Django, which means it must be happening elsewhere, perhaps in Apache?
[编辑] 从下面的帖子来看,似乎在 Django 中禁用了缓存,这意味着它必须发生在其他地方,也许在 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.
[编辑] 我对正在发生的事情有更详尽的描述:对于向服务器发出的前 7 个(左右)请求,页面由脚本呈现并返回,尽管这 7 个页面中的每一个似乎都被缓存了稍后出现。在第 8 个请求时,服务器提供第一页。在第 9 个请求时,它提供第二个页面,以此类推。这一直持续到我重新启动 apache 时,该过程再次开始。
[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.
[编辑] 我已将 mod_wsgi 配置为一次只运行一个进程,这会导致计时器在每种情况下都重置为相同的值。有趣的是,我的页面上还有另一个组件,它使用 order('?') 在每个请求上显示随机图像,并且每次刷新不同的图像,这表明缓存发生在 Django 而不是在 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.
[编辑]根据之前的编辑,我回去查看了相关的views.py文件,发现倒计时开始变量是在模块中全局设置的,在视图函数之外。在视图函数中移动该设置解决了问题。所以结果证明它毕竟不是缓存问题。感谢大家在这方面的帮助。
采纳答案by John Paulett
From my experience with mod_wsgi in Apache, it is highly unlikely that they are causing caching. A couple of things to try:
根据我在 Apache 中使用 mod_wsgi 的经验,它们极不可能导致缓存。有几件事可以尝试:
- It is possible that you have some proxy serverbetween 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.
- Can you post your MIDDLEWARE_CLASSES from your settings.py file. It possible that you have a Middleware that performs caching for you.
- Can you grep your code for the following items "load cache", "django.core.cache", and "cache_page". A *grep -R "search" ** will work.
- Does the settings.py (or anything it imports like "from localsettings import *") include CACHE_BACKEND?
- 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)
- 您的计算机和 Web 服务器之间可能有一些代理服务器,可以适当或不适当地缓存页面。有时 ISP 会运行代理服务器以减少其网络外的带宽。您能否为正在缓存的页面提供 HTTP 标头(Firebug 可以为您提供这些标头)。我特别感兴趣的标头包括 Cache-Control、Expires、Last-Modified 和 ETag。
- 你能从你的 settings.py 文件中发布你的 MIDDLEWARE_CLASSES 吗?您可能有一个为您执行缓存的中间件。
- 您能否为以下项目“加载缓存”、“django.core.cache”和“cache_page”grep 代码。A *grep -R "search" ** 将起作用。
- settings.py(或它导入的任何内容,例如“从 localsettings import *”)是否包含 CACHE_BACKEND?
- 当你重新启动 apache 时会发生什么?(例如 sudo 服务 apache 重启)。如果重新启动清除了问题,那么它可能是 apache 进行缓存(这也可能清除 locmen Django 缓存后端)
回答by Graham Dumpleton
Are you using a multiprocess configuration for Apache/mod_wsgi? If you are, that will account for why different responses can have a different value for the timer as likely that when timer is initialised will be different for each process handling requests. Thus why it can jump around.
您是否正在为 Apache/mod_wsgi 使用多进程配置?如果是,这将解释为什么不同的响应可以具有不同的计时器值,因为每个处理请求的进程初始化计时器的时间可能不同。因此为什么它可以跳来跳去。
Have a read of:
读一读:
http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading
http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading
Work out in what mode or configuration you are running Apache/mod_wsgi and perhaps post what that configuration is. Without knowing, there are too many unknowns.
确定您正在运行 Apache/mod_wsgi 的模式或配置,并可能发布该配置是什么。在不知道的情况下,还有太多的未知数。
回答by Brad
I just came across this:
我刚刚遇到了这个:
Support for Automatic Reloading To help deployment tools you can activate support for automatic reloading. Whenever something changes the .wsgi file, mod_wsgi will reload all the daemon processes for us.
For that, just add the following directive to your Directory section:
支持自动重新加载 为了帮助部署工具,您可以激活对自动重新加载的支持。每当 .wsgi 文件发生变化时,mod_wsgi 都会为我们重新加载所有守护进程。
为此,只需将以下指令添加到您的目录部分:
WSGIScriptReloading On
回答by PKKid
Did you specifically setup Django caching? From the docs it seems you would clearly know if Django was caching as it requires work beforehand to get it working. Specifically, you need to define where the cached files are saved.
您是否专门设置了 Django 缓存?从文档中,您似乎可以清楚地知道 Django 是否正在缓存,因为它需要事先工作才能使其正常工作。具体来说,您需要定义缓存文件的保存位置。

