by May 20, 2015

In one of our applications, we have some controller code that looks similar to:

1
2
3
@routes = Rails.cache.fetch 'routes', expires_in: 6.hours do
  Routes.all
end

Although we had controller caching turned off in our development configuration, we were still seeing cached results locally. So, when we looked in the environment, it was confusing to see a setting for turning caching off, but caching still taking place.

Caching Gotcha

Turns out that interacting with Rails.cache directly will not check if caching is disabled. Additionally, since the default cache_store is set to a file store in development, there is indeed a cache available.

In order to get around this, we needed to set the cache store to something that would always result in a cache miss. Enter NullStore:

# Disable caching for Rails.cache.fetch
config.cache_store = [:null_store]

I almost feel like this should be the default for the development (and possibly test) environment settings. If you desire caching in those environments, then it seems like an exception and not the rule.

Anyway, if you've encountered this problem, hopefully this post helps!

Curtis Miller

Curtis Miller

Managing Partner

Startup junkie, Rubyist and gamer. Loves to brainstorm about new ideas.

Need help with your project?

We specialize in Ruby on Rails and JavaScript projects. Code audits, maintenance and feature development on existing apps, or new application development. We've got you covered.

Get in touch!