title: ruby on rails: Rails.cache.write returning false for array of specific object fields
body: <p>I'm trying to save a large array of ids and permalinks for approved pages in memcache so that I don't have to hit the database multiple times for the same data.</p>
  
  <p>My code:</p>
  
  <pre><code>if Rails.cache.exist?("ids_and_permalinks_array")
    data = Rails.cache.fetch("ids_and_permalinks_array")
  else
    data = []
  
    Page.approved.select('id, permalink').find_each { |f| data &lt;&lt; f }
  
    Rails.cache.write("ids_and_permalinks_array", data, :expires_in =&gt; 12.hours)
  end
  </code></pre>
  
  <p><code>approved</code> on <code>Page</code> is a simple <code>where(:approved =&gt; true)</code>, and data is a large array of object subsets like <code>#&lt;Page id: 1, permalink: "page-permalink-1"&gt;</code> (the array is around 50,000 objects).</p>
  
  <p>The <code>Rails.cache.write</code> line returns <code>false</code> when run from the console, with the development.log showing: <code>Cache write: ids_and_permalinks_array ({:expires_in=&gt;43200 seconds})</code></p>
  
  <p>So, the log says it's writing to the cache, but <code>Rails.cache.fetch("ids_and_permalinks_array")</code> returns <code>nil</code>.</p>
  
  <p>Suggestions?  Anything obvious I'm doing wrong?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've also tried this, and still don't get the value written to cache:</p>
  
  <pre><code>Rails.cache.fetch("ids_and_permalinks_array", :expires_in =&gt; 12.hours, :race_condition_ttl =&gt; 10.minutes) do
    Page.approved.select('id, permalink').find_each { |p| data &lt;&lt; p }
  end
  </code></pre>
  
  <p>** UPDATE 2 **</p>
  
  <p>I added <code>Rails.logger.info("\n#{Rails.cache.exist?("ids_and_permalinks_array")\n")</code> to the beginning and end of the method call this code is in.  Each time, at the beginning of the method call, it logs <code>false</code> and at the end it logs <code>true</code>...  So, it's working, but is it only setting for that thread / instance of the method call?</p>