Rails:预定任务来预热缓存?

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

caches_action :complex_report, :expires_in => 1.day 

控制器操作受Devise身份validation保护。

页面当前在用户第一次请求时被缓存。 然后从缓存中提取当天的后续请求。

这个问题是初始请求需要20-30秒才能加载。 是否可以通过计划任务预先填充缓存?

任何建议非常感谢。

以下是对以前基于cron的解决方案的扩展,该解决方案使用curl存储cookie的能力,以便您可以在一个步骤中进行身份validation,然后在下一步中再次使用cookie作为经过身份validation的用户。 所以,如果你把这些行放在名为“prepare_cache.sh”的脚本中

 rm /tmp/cookiejar curl --request POST -d "login=" -d "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。 我之前删除了cookiejar,以确保那里没有文件,并在最后将其删除,以确保没有一个cookie,它不应该具有访问级别。

然后你可以用cron作业调用这个脚本:

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

希望这应该有效。 当我尝试它时,似乎为我工作。

可能最基本的解决方案是设置一个简单的cron条目来加载你想要拥有“热”缓存的页面。 这可以很容易地将以下内容添加到服务器上用户的crontab ,使用crontab -e打开编辑器:

*/15 * * * * wget -q http://yourwebpages.url/ > /dev/null 2>&1

这样做是使用wget每隔15小时,每天,每月和每年在提供的URL上获取数据,忽略结果,如果出现问题则不发送* nix邮件。

如果是运行报告和收集耗时的结果的过程,您可以使用Rails.cache.writeRails.cache.read缓存这些结果(代替或沿着动作缓存)。

然后,因为您不必担心身份validation或向服务器发出请求,所以运行查询和缓存来自cron作业的结果的行为会相当简单。

看看这个gem:

https://github.com/tommyh/preheat

gem用于预热Rails.cache。

从文档中: This will "preheat" all your Rails.cache.fetch calls on your homepage. It is as simple as that! This will "preheat" all your Rails.cache.fetch calls on your homepage. It is as simple as that!

  #app/models/product.rb def slow_method Rails.cache.fetch("product-slow-method-#{self.id}") do sleep 15 Time.now end end #lib/tasks/preheat.rake namespace :preheat do desc "Preheat product caches" task (:products => :environment) do Preheat.it do Product.all.each do |product| app.get(app.products_path(product)) #or you could just call product.slow_method directly, whatever makes more sense end end end end #crontab -e 0 * * * * /path/to/rake preheat:products RAILS_ENV=production 2>&1 >> #{Rails.root}/log/preheat.log &