如何禁用 Django / mod_WSGI 页面缓存

发布于 2024-08-08 16:23:49 字数 863 浏览 5 评论 0原文

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

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

当用户刷新页面或使用倒计时器导航到不同页面时,就会出现问题。计时器似乎偶尔会跳转到不同的时间,通常在每次刷新时都会一遍又一遍地返回到同一时间。

使用 HTTPFox,该页面不会从我的浏览器缓存中加载,因此看起来 Django 或 Apache 正在缓存该页面。有什么方法可以禁用此功能吗?我不会有足够的流量来担心缓存脚本输出。或者我对于为什么会发生这种情况完全错误吗?

[编辑] 从下面的帖子来看,Django 中似乎禁用了缓存,这意味着它一定发生在其他地方,也许在 Apache 中?

[编辑]我对正在发生的事情有更全面的描述:对于向服务器发出的前 7 个(左右)请求,页面由脚本呈现并返回,尽管这 7 个页面中的每一个似乎都被缓存了稍后出现。在第 8 个请求时,服务器提供第一页。在第 9 个请求时,它提供第二个页面,依此类推。这将持续到我重新启动 apache,此时该过程再次开始。

[编辑] 我已将 mod_wsgi 配置为一次仅运行一个进程,这会导致计时器在每种情况下重置为相同的值。但有趣的是,我的页面上还有另一个组件,它使用 order('?') 在每个请求上显示随机图像,并且每次都会刷新不同的图像,这表明缓存发生在 Django 中而不是 Apache 中。

[编辑]根据之前的编辑,我回去查看了相关的views.py文件,发现倒计时开始变量是在模块中全局设置的,在视图函数之外。将该设置移至视图函数内解决了问题。所以事实证明这毕竟不是缓存问题。感谢大家对此的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瑶笙 2024-08-15 16:23:49

根据我在 Apache 中使用 mod_wsgi 的经验,它们不太可能导致缓存。有几点需要尝试:

  1. 您的计算机和网络之间可能有一些代理服务器适当或不适当地缓存页面的服务器。有时 ISP 会运行代理服务器来减少网络外部的带宽。您能否提供正在缓存的页面的 HTTP 标头(Firebug 可以将这些标头提供给您)。我特别感兴趣的标头包括 Cache-Control、Expires、Last-Modified 和 ETag。
  2. 您可以从 settings.py 文件中发布您的 MIDDLEWARE_CLASSES 吗?您可能有一个为您执行缓存的中间件。
  3. 您可以在代码中 grep 查找以下项目“load cache”、“django.core.cache”和“cache_page”吗? *grep -R“搜索”** 可以工作。
  4. settings.py(或者它导入的任何内容,例如“from localsettings import *”)是否包含CACHE_BACKEND?
  5. 重新启动 apache 时会发生什么? (例如 sudo services apache restart)。如果重新启动可以解决问题,那么可能是 apache 正在执行缓存(这也可能会清除 locmen Django 缓存后端)

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)
无尽的现实 2024-08-15 16:23:49

您是否专门设置了 Django 缓存?从文档看来,您似乎可以清楚地知道 Django 是否正在缓存,因为它需要事先进行工作才能使其正常工作。具体来说,您需要定义缓存文件的保存位置。

http://docs.djangoproject.com/en/dev/topics/cache/< /a>

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.

http://docs.djangoproject.com/en/dev/topics/cache/

甜是你 2024-08-15 16:23:49

您是否正在为 Apache/mod_wsgi 使用多进程配置?如果是的话,这将解释为什么不同的响应可以有不同的计时器值,因为当初始化计时器时,每个处理请求的进程都会有所不同。这就是为什么它可以跳来跳去。

请阅读:

http://code.google.com/p/modwsgi/wiki /ProcessesAndThreading

弄清楚您正在运行 Apache/mod_wsgi 的模式或配置,并可能发布该配置是什么。不知道,有太多的未知。

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.

Have a read of:

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.

瞄了个咪的 2024-08-15 16:23:49

我刚刚遇到这个:

支持自动重新加载以帮助您可以部署工具
激活对自动重新加载的支持。每当事情发生变化时
.wsgi 文件中,mod_wsgi 将为我们重新加载所有守护进程。

为此,只需将以下指令添加到您的目录部分:

WSGIScriptReloading On

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:

WSGIScriptReloading On
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文