Rails: scheduled task to warm up the cache?

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

caches_action :complex_report, :expires_in => 1.day 

The action of the controller is protected by authentication.

The page is currently cached for the first time the user requests it. A subsequent request that day is pulled from the cache.

The problem is that the initial request takes 20-30 seconds to load. Is it possible to pre-populate the cache using a scheduled task?

Any suggestions that are very much appreciated.

+6
caching ruby-on-rails ruby-on-rails-3 memcached
source share
4 answers

Here is the extension on the previous cron-based solution that uses curl to store cookies so you can complete one step and then use the cookie again as an authenticated user in the next step. Therefore, 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 

replacing the login and password options with those that match the variables used in your login form, and obviously the URLs. I delete the cookiejar earlier to make sure that there is no file and delete it at the end, to make sure there is no cookie floating with access levels that it should not have.

Then you can call this script with the cron job:

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

And hopefully this should work. It seemed like I was working for me when I tried.

+5
source share

Perhaps the most basic solution would be to create a simple cron entry to load the page in which you want to have a hot cache. This can be easily added to the crontab user on your server using crontab -e to open the editor:

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

What this will do is use wget to retrieve data from the provided URL every 15 minutes of every hour, day, month and year, ignore the results and do not send * nix mail in case something goes wrong.

+3
source share

If this is the process of running a report and collecting time-consuming results, you can cache these results (instead of caching actions or along the side) using Rails.cache.write and Rails.cache.read .

Then, since you don’t have to worry about authentication or querying the server, the action of running the query and caching the results from the cron job will be much simpler.

+3
source share

Take a look at this stone:

https://github.com/tommyh/preheat

Pearls are designed to pre-heat your Rails.cache.

From the documentation: 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 & 
+3
source share

All Articles