title: Rails - how to cache data for server use, serving multiple users
body: <p>I have a class method (placed in    <code>/app/lib/</code>) which performs some heavy calculations and sub-http requests until a result is received.</p>
  
  <p>The result isn't too dynamic, and requested by multiple users accessing a specific view in the app.</p>
  
  <p>So, I want to schedule a periodic run of the method (using cron and Whenever gem), store the results somewhere in the server using JSON format and, by demand, read the results alone to the view.</p>
  
  <p>How can this be achieved? what would be the <strong>correct</strong> way of doing that?</p>
  
  <p>What I currently have:</p>
  
  <pre><code>def heavyMethod
     response = {}
     # some calculations, eventually building the response
     File.open(File.expand_path('../../../tmp/cache/tests_queue.json', __FILE__), "w") do |f|
        f.write(response.to_json)
     end
  end
  </code></pre>
  
  <p>and also a corresponding method to read this file.</p>
  
  <p>I searched but couldn't find an example of achieving this using Rails cache convention (and not some private code that I wrote), on data which isn't related with ActiveRecord.</p>
  
  <p>Thanks!</p>