如何禁用 Django / mod_WSGI 页面缓存
我通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据我在 Apache 中使用 mod_wsgi 的经验,它们不太可能导致缓存。有几点需要尝试:
From my experience with mod_wsgi in Apache, it is highly unlikely that they are causing caching. A couple of things to try:
您是否专门设置了 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/
您是否正在为 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.
我刚刚遇到这个:
I just came across this: