Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Finally found a question and answer:<br> Q: <a href="https://stackoverflow.com/questions/543961/warm-up-cache-on-deployment">&quot;Warm Up Cache&quot; on deployment</a><br> A: <a href="https://stackoverflow.com/a/942774/593053">https://stackoverflow.com/a/942774/593053</a><br></p> <p>Leaving my question here so that others can follow the trail and learn from my pain.</p> <p>Also relevant: <a href="https://stackoverflow.com/questions/9423511/rails-3-2-pre-render-bake-a-new-page-cache-immediately-after-expiry">Rails 3.2: Pre-render (bake) a new page cache immediately after expiry?</a></p> <p>My full solution was to adopt the convention that if <code>cache=regen</code> is in a requested URL, that means that the generated page should be stuffed into the cache.</p> <p>For controllers for which I want caching, <code>include CacheRegen</code>. <code>CacheRegen</code> causes the controller to not read from cache when cache=regen, and to not put cache=regen into the key when storing in the cache.</p> <p>The code for which is:</p> <pre><code>module CacheRegen def read_fragment(key, options = nil) if /cache=regen/.match(key) logger.info("forcing cache miss due to param cache=regen, key=#{key}") return nil end super(key, options) end def write_fragment(key, content, options = nil) unless key.sub!(/cache=regen/, '').nil? key.sub!(/\?\&amp;/, '?') key.sub!(/\&amp;\&amp;/, '&amp;') key.sub!(/\?$/, '') key.sub!(/\&amp;$/, '') logger.info("wrote page to cache with key #{key}") end super(key, content, options) end end </code></pre> <p>Finally, I put the following code in new_pages.rake:</p> <pre><code>require 'action_dispatch' def get_url(sess, url) uri = "http://YOURSITE.com/" + url + "cache=regen" puts "retrieving " + uri foo = sess.get(uri) puts "got it. #{foo}, #{sess.response.body.length} bytes" end desc "If necessary, generate new versions of the most expensive pages" task :new_pages =&gt; :environment do puts "Updating pages..." sess = ActionDispatch::Integration::Session.new(Rails.application) ["controller1", "controller2", "controller3"].each { |noun| get_url(sess, noun + "?") } puts "done." end </code></pre> <p>And in my environment, I have a <code>deploy</code> task which depends on the <code>new_pages</code> task.</p> <p>Is there some gem out there that makes all this more automagical?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload