Blog
Posts tagged with "memory_store".
Oddity with Rails Memory Store cache
Sid
25 May 2010 20:47
A bit of a specialist post, but I was caught out today by odd behaviour with the using the memory_store cache in Rails.
This is a code snippet from our controller method
cache_key = 'current_standings' @standings = Rails.cache.read(cache_key) # check cache if @standings.nil? # ... fetch the data # add to the cache Rails.cache.write(cache_key,@standings) end
The problem I found was that the second time through, i.e. on the cache read, the items in the cache were nil.
When I tested the items in the cache there were the number expected, and they were the right type but they were just not populated. Putting the object in and out of the cache in the console was fine.
In the end, and after a lot of error and some trial, I found that I needed to set the config.cache_classes property to true in the development.rb file. An unfortunate side-effect of this is that hot deploy is no longer hot and I need to restart the server to pick up changes but at least the cache works okay.
I’m guessing that with each request the cache (or at least the variables in there were being blasted) but would be interested in hearing from anyone else who has experienced the same/similar.

