Partial Caching is a great way to get some major page speed improvements without a lot of work. But if you forget a touch: true on one of your associations, or your template dependencies aren’t working right, your cached partials won’t get updated.

Since the development environment usually runs with caching disabled, you’ll only discover this in staging, or even worse, production! To debug the problem, you’ll need to reproduce it in development mode by setting

config/environments/development.rb
1
config.action_controller.perform_caching             = false

in your config/environments/development.rb to true. You’ll have to do this every time you have to debug cache problems, and you’ll have to remember to change it back before you check in. I hate to do that kind of stuff, so when I start a new project, I set it to this instead:

config/environments/development.rb
1
config.action_controller.perform_caching             = ENV['CACHING'] == 'true'

That way, I can enable caching whenever I need to by starting my Rails server with

1
CACHING=true rails server

Running just rails server will run with caching disabled, as usual.

As you start tweaking configuration parameters more often, this pattern of turning hardcoded parameters into environment variables can save you a lot of time and fiddling. Give it a try in your own projects!

Don't miss out on my next essay

Sign up below to get my free weekly Ruby column. I'll send you original articles and advice every Friday to help make you a smarter, better Ruby developer. Drop your name in the box!

Did you like this post? You should read these:

Comments