Rails:预定任务来预热缓存吗? [英] Rails: Scheduled task to warm up the cache?

查看:87
本文介绍了Rails:预定任务来预热缓存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下内容使用memcached来缓存加载缓慢的页面:

I am using the following to cache a slow loading page using memcached:

caches_action :complex_report, :expires_in => 1.day

控制器动作受Devise身份验证的保护.

The controller action is protected by Devise authentication.

该页面当前在用户第一次请求时被缓存.然后将当天的后续请求从缓存中拉出.

The page currently gets cached the first time a user requests it. Subsequent request that day are then pulled from the cache.

此问题是初始请求需要20到30秒的加载时间.是否可以通过计划任务预先填充缓存?

The problem with this is that the initial request takes 20-30 seconds to load. Is it possible to populate the cache in advance by way of a scheduled task?

任何建议都值得赞赏.

推荐答案

此处是对以前基于cron的解决方案的扩展,该解决方案使用curl的功能来存储cookie,以便您可以在一个步骤中进行身份验证,然后将cookie再次用作在下一步中通过身份验证的用户.因此,如果将这些行放在名为"prepare_cache.sh"的脚本中

Here is an expansion on the previous cron based solution which uses curl's ability to store cookies so that you can auth in one step and then use the cookie again as an authenticated user in the next step. So if you put these lines in a script called "prepare_cache.sh"

rm /tmp/cookiejar
curl --request POST -d "login=<username>" -d "password=<password>" -c /tmp/cookiejar http://yourwebpages.url/login
curl --request GET -b -c /tmp/cookiejar http://yourwebpages.url/page_to_cache
rm /tmp/cookiejar

用与登录表单中使用的变量以及要调用的URL匹配的参数替换login和password参数.我先删除cookiejar,以确保那里没有文件,最后删除它,以确保没有cookie随其访问级别而浮动.

replacing the login and password parameters with ones which match the variables used in your login form and obviously the urls to call. I'm removing the cookiejar before to make sure there isn't a file there already and removing it at the end to make sure there isn't a cookie floating about with access levels it shouldn't have.

然后您可以使用cron作业调用此脚本:

Then you can call this script with the cron job:

*/15 * * * * /home/myname/prepare_cache.sh > /dev/null 2>&1

希望这应该可行.尝试时似乎为我工作.

And hopefully that should work. Seemed to work for me when I tried it.

这篇关于Rails:预定任务来预热缓存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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