18

In development, the following (simplified) statement always logs a cache miss, in production it works as expected:

@categories = Rails.cache.fetch("categories", :expires_in => 5.minutes) do
  Rails.logger.info "+++ Cache missed +++"
  Category.all
end

If I change config.cache_classes from false to true in config/development.rb, it works as well in development mode, however, this makes development rather painful. Is there any configuration setting that is like config.cache_classes = false except that Rails.cache.fetch is fetching from cache if possible?

Markus Proske
  • 3,356
  • 3
  • 24
  • 32

1 Answers1

34

Try placing the following in /config/environments/development.rb:

# Temporarily enable caching in development (COMMENT OUT WHEN DONE!)
config.action_controller.perform_caching = true

Additionally, if your cache store configuration is in /config/environments/production.rb, then you will need to copy the appropriate line into development.rb as well. For example, if your cache store is the Dalli memcache gem:

# copied from production.rb into development.rb for caching in development
config.cache_store = :dalli_store, '127.0.0.1' 

Hope that helps.

ms-ati
  • 1,297
  • 1
  • 13
  • 17
  • 10
    Just a bonus tip, add an unless ENV["DEV_CACHE"] put the testing config in it, with an else that has the normal non-caching config in it. Then whenever you want to enable caching in your terminal, you can write export DEV_CACHE="ANYTHING". Best part is, you're way less like to accidentally have caching enabled when you don't want it. – tehprofessor Mar 12 '14 at 21:22
  • 2
    This tip is super useful together with the solution. – anbiniyar Mar 15 '14 at 06:24