Django缓存 - 可以先发制人吗? [英] Django caching - can it be done pre-emptively?

查看:156
本文介绍了Django缓存 - 可以先发制人吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django视图,它从外部网站接收部分数据,我使用urllib2 / BeautifulSoup解析。



这个操作是相当昂贵的,所以我使用低级缓存API缓存约5分钟。然而,在缓存数据到期后访问该站点的每个用户将在我去外部站点解析新数据时将会收到几秒钟的显着延迟。



有没有办法懒惰地加载新的数据,以至于没有用户会遇到这样的延迟?或者这是不可避免的?



请注意,我在一个共享托管服务器上,所以请记住你的答案。



编辑:感谢您的帮助。但是,我仍然不确定如何使用我将要调用的python脚本来完成此操作。我做的一个基本测试表明,django缓存不是全局的。意思是,如果我从一个外部脚本调用它,它在框架中看不到缓存数据。建议?



另一个编辑:来想想,这可能是因为我还在使用本地内存缓存。我怀疑如果我将缓存移动到memcached,DB,无论如何,这将被解决。

解决方案

所以你想安排要定期运行的东西?以某些CPU时间为代价,您可以使用这个简单的应用



或者,如果可以使用它, cron工作每5分钟是:

  * / 5 * * * * / path /to/project/refresh_cache.py 

Web主机提供了不同的设置方式。对于cPanel,请使用Cron Manager。对于Google App Engine,请使用 cron.yaml 。对于所有这些,您需要设置环境 refresh_cache.py 中。



顺便说一句,响应用户的请求被认为是延迟缓存。这是先发制人的缓存。并且不要忘记缓存足够长的时间来重新创建页面。


I have a Django view, which receives part of its data from an external website, which I parse using urllib2/BeautifulSoup.

This operation is rather expensive so I cache it using the low-level cache API, for ~5 minutes. However, each user which accesses the site after the cached data expires will receive a significant delay of a few seconds while I go to the external site to parse the new data.

Is there any way to load the new data lazily so that no user will ever get that kind of delay? Or is this unavoidable?

Please note that I am on a shared hosting server, so keep that in mind with your answers.

EDIT: thanks for the help so far. However, I'm still unsure as to how I accomplish this with the python script I will be calling. A basic test I did shows that the django cache is not global. Meaning if I call it from an external script, it does not see the cache data going on in the framework. Suggestions?

Another EDIT: coming to think of it, this is probably because I am still using local memory cache. I suspect that if I move the cache to memcached, DB, whatever, this will be solved.

解决方案

So you want to schedule something to run at a regular interval? At the cost of some CPU time, you can use this simple app.

Alternatively, if you can use it, the cron job for every 5 minutes is:

*/5 * * * * /path/to/project/refresh_cache.py

Web hosts provide different ways of setting these up. For cPanel, use the Cron Manager. For Google App Engine, use cron.yaml. For all of these, you'll need to set up the environment in refresh_cache.py first.

By the way, responding to a user's request is considered lazy caching. This is pre-emptive caching. And don't forget to cache long enough for the page to be recreated!

这篇关于Django缓存 - 可以先发制人吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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