title: How can I set up some type of Task or Initializer to load data in my Memcache on Heroku
body: <p>So I just pushed by first app up to Heroku for the first time and discovered that the initializer file I use in development to load up data in my cache was causing a recompile error so I "fixed" it with this dumb hack you can see <a href="https://github.com/MKwenhua/petecvHeroku/blob/master/config/initializers/querycache.rb" rel="nofollow">here</a> </p>
  
  <p>the file <strong>querycache.rb</strong></p>
  
  <pre><code>@songs = Song.all   
  
  @songs.each do |sng|
      Rails.cache.fetch(sng.song_title){sng}
  end
  top100 =  Song.where('num_stars &gt; ?', 0 ).order('num_stars desc, total_score desc').includes(:categories).limit(100)
  Rails.cache.fetch('Top100'){top100} 
  
  trending_users = User.where('followers_count &gt; ?', 0 ).order("reputation  desc").limit(10)
   Rails.cache.fetch('cool_crowd', :expires_in =&gt; 12.hours){trending_users} 
   newest_songs = Song.last(8)
   Rails.cache.fetch('newest_songs'){newest_songs} 
   just_joined = User.last(8)
   Rails.cache.fetch('newest_users'){just_joined} 
  </code></pre>
  
  <p>I have read Heroku's documentation on caching, but this situation is a tad bit different. Although I do have more dynamic caching like fragment caching and other small optimizations, given the nature of my app it's important that I have all <strong>:song_title</strong> strings and objects stored in the cache so that my app can quickly process requests. So if there is anything such as maybe a rake task that can execute the code above, or some other way of getting these objects loaded in the cache when the server is up and running that would be great. </p>
  
  <p>Sorry for the newbie question, I am still kind of new to rails and while I know there is an easy alternative, I am not exactly sure what it is. </p>
  
  <p><strong>EDIT: I Found a solution to my problem</strong></p>
  
  <p>Ok so in the <strong>application.rb</strong> file within the config folder I added this: </p>
  
  <pre><code>class Application &lt; Rails::Application
      config.after_initialize do
        @songs = Song.all 
  
        @songs.each do |sng|
         Rails.cache.fetch(sng.song_title){sng}
        end
        top100 =  Song.where('num_stars &gt; ?', 0 ).order('num_stars desc, total_score desc').includes(:categories).limit(100).to_a
        Rails.cache.fetch('Top100'){top100} 
  
        trending_users = User.where('followers_count &gt; ?', 0 ).order("reputation  desc").limit(10).to_a
        Rails.cache.fetch('cool_crowd', :expires_in =&gt; 12.hours){trending_users} 
        newest_songs = Song.last(8)
        Rails.cache.fetch('newest_songs'){newest_songs} 
        just_joined = User.last(8)
        Rails.cache.fetch('newest_users'){just_joined} 
      end
  end
  </code></pre>
  
  <p>anyways, the objects I wanted to load in the initializer i just moved to the application file with the block command  <strong>config.after_initialize do</strong>. Now everything seems to work fine. </p>