Preheat or generate fragment cache on deploy – Rails 5




If you have a page that takes a lot of time to generate the cache. The first user who stumbles upon it is in pain. Sometimes it times out before it gets generated completed.


So, I created a method in one of the helper class



def self.recreate_cache


the_controller = ActionController::Base.new
# Set any instance variables required by your partial in the controller,
# they will be passed to the partial with the view_context reference
the_controller.instance_variable_set "@users", User.all


view_renderer = ActionView::Renderer.new the_controller.lookup_context
view_renderer.render the_controller.view_context, {template: 'users/index.html.erb'}


end


This will render all the fragment cache.


Hook this code in capistrano’s deploy method and it should be able to generate it every time you deploy. You could wrap it in a delayed job if you need your deploys faster.







Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?