Debug a Stuck Cache With a Simple Code Change

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
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
config.action_controller.perform_caching             = ENV['CACHING'] == 'true'

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

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!

Pushing through tutorials, and still not learning anything?

Have you slogged through the same guide three times and still don't know how to build a real app?

In this free 7-day Rails course, you'll learn specific steps to start your own Rails apps — without giving up, and without being overwhelmed.

You'll also discover the fastest way to learn new Rails features with your 32-page sample of Practicing Rails: Learn Rails Without Being Overwhelmed.

Sign up below to get started:

Powered by ConvertKit

Did you like this article? You should read these:

Comments