My friend Calvin (IntoMobile Developer) and I were just talking about keeping your app from caching with Rails.cache in development mode. One nice way to do it is to wrap all your Rails.cache calls in your application_controller.rb. In this wrapping call, you can determine whether or not to execute the caching or to just let it through in development. Another benefit is being able to change your caching behavior in the future without needing to mess around with every single line of code that you've written for caching.
In my development.rb file, I define the following:
I am using the memcache gem, rather than the more popular memcache-client gem, and running memcached on my development box as well (I like testing with it running, rather than using the built in rails way of storing it in memory). The reason for using memcache gem is because I'm currently hosting our app on Heroku, which requires this gem to work in tandem with their cloud solution for memcached.
The CACHE constant is setup to use this memcache gem's caching mechanism, which would be equivalent to the built in Rails.cache. Setting this constant up allows me to change this in my environment files once, without ever needing to worry about all the places I set it up.
Then there's the cache_store, which I set correctly to use Memcached gem. This will help me store the cache correctly.
CACHE_DEVELOPMENT constant is for me to turn caching on / off for development mode. You'll see how it works in the next piece of code below.
The last two lines that are commented out basically control action and class caching, which doesn't really effect if Rails.cache even goes through. So just setting these two things to false, Rails.cache will still try to cache. In order to keep this from happening, we have to do some logic in our application controller wrapper, here it is:
The cache_fetch method will be called everytime I want a cache call from a controller. I pass in the cache_key as the first parameter to basically name this cache result, and I also accept the period of time the cache is kept before it expires. 0 means it never expires, so that is the default value.
The conditional statement basically allows me to turn caching on or off. If I'm in the development environment and the constant CACHE_DEVELOPMENT is set to false, I just let the block through without hitting any caching. Otherwise it goes into the caching logic.
Check out that # CACHE.fetch(key, time_expire){yield} block. This is how it used to work, but recently a few things changed so that I needed to do a get, and if that didn't exist I'd have to manually set it. I'm not sure why this happened, or if it's still needed, but you can see how I was able to change this detail in one place, rather than everywhere I tried to cache.
The rest shows you how I'm caching. This would be ugly if I had to do all of that everytime I wanted to cache a result, and even worse if something changed. This way I can control the development environment for testing caching, as well as keeping code as DRY as possible.
Here's a simple example of how I call the cache_fetch action from my controller:
Hope that helps!