Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Welcome to one of the two hard problems in computer science, cache invalidation :)</p> <p>You would have to handle that manually since the logic for when a cached object, unlike a cached view which can be simply derived from the objects it displays, should be invalidated is application and situation dependent.</p> <p>You goto method for this is the <code>Rails.cache.fetch</code> method. <code>Rails.cache.fetch</code> takes 3 arguments; the cache key, an options hash, and a block. It first tries to read a valid cache record based on the key; if that key exists and hasn’t expired it will return the value from the cache. If it can’t find a valid record it instead takes the return value from the block and stores it in the cache with your specified key.</p> <p>For example:</p> <pre><code>@models = Rails.cache.fetch my_cache_key do Model.where(condition: true).all end </code></pre> <p>This will cache the block and reuse the result until something (tm) invalidates the key, forcing the block to be reevaluated. Also note the <code>.all</code> at the end of the method chain. Normally Rails would return an ActiveRecord relation object that would be cached and this would then be evaluated when you tried to use <code>@models</code> for the first time, neatly sidestepping the cache. The <code>.all</code> call forces Rails to eager load the records and ensure that it's the result that we cache, not the question.</p> <p>So now that you get all your cache on and never talk to the database again we have to make sure we cover the other end, invalidating the cache. This is done with the <code>Rails.cache.delete</code> method that simply takes a cache key and removes it, causing a miss the next time you try to fetch it. You can also use the <code>force: true</code>option with fetch to force a re-evaluation of the block. Whichever suits you.</p> <p>The science of it all is where to call <code>Rails.cache.delete</code>, in the naïve case this would be on update and delete for a single instance and update, delete, create on any member for a collection. There will always bee corner cases and they are always application specific, so I can't help you much there.</p> <p>I assume in this answer that you will set up some sane cache store, like memcached or Redis. </p> <p>Also remember to add this to config/environments/development.rb:</p> <pre><code>config.cache_store = :null_store </code></pre> <p>or you development environment will cache and you will end up hairless from frustration.</p> <p>For further reference read: <a href="http://robotmay.tumblr.com/post/23161612605/everyone-should-be-using-low-level-caching" rel="nofollow">Everyone should be using low level caching in Rails</a> and <a href="http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html" rel="nofollow">The rails API docs</a></p> <p>It is also worth noting that functionality is not removed from Rails 4, merely extracted into a gem. If you need or would like the full features of the sweepers simply add it back to your app with a <code>gem 'rails-observers'</code> line in your Gemfile. That gem contains both the sweepers and observers that where removed from Rails 4 core.</p> <p>I hope that helpt you get started.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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