Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe <code>Rails.cache</code> uses the <code>ActiveSupport::Cache::Store</code> interface, which has a <code>read_multi</code> method for this exact purpose. <a href="http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html#method-i-read_multi" rel="noreferrer">[1]</a></p> <p>I think swapping out <code>fetch</code> for <code>read_multi</code> will improve your performance because <code>ActiveSupport::Cache::MemCacheStore</code> has an optimized implementation of <code>read_multi</code>. <a href="http://api.rubyonrails.org/classes/ActiveSupport/Cache/MemCacheStore.html#method-i-read_multi" rel="noreferrer">[2]</a></p> <p><strong>Code</strong></p> <p>Here's the updated implementation:</p> <pre><code>keys = @messages.collect { |m| "message/#{m.id}/#{m.updated_at.to_i}" } hits = Rails.cache.read_multi(*keys) keys.each_with_index do |key, i| if hits.include?(key) @messages[i] = hits[key] else Rails.cache.write(key, @messages[i] = @messages[i].as_json) end end </code></pre> <p>The cache writes are still performed synchronously with one round trip to the cache for each miss. If you want to cut down on that overhead, look into running background code asynchronously with something like <a href="https://github.com/purzelrakete/workling" rel="noreferrer">workling</a>. </p> <p>Be careful that the overhead of starting the asynchronous job is actually less than the overhead of <code>Rails.cache.write</code> before you start expanding your architecture.</p> <p><strong>Memcached Multi-Set</strong></p> <p>It looks like the Memcached team has at least considered providing Multi-Set (batch write) commands, but there aren't any ActiveSupport interfaces for it yet and it's unclear what level of support is provided by implementations. <a href="http://code.google.com/p/memcached/wiki/NewCommonFeatures#Multi-Set" rel="noreferrer">[3]</a></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