BinaryWebPark

Rails Cache: Local Testing Tricks

January 31, 2017

Rails Cache: Local Testing

One great feature of Rails is how easy it makes caching. Recently, I was working on an application that needed to load thousands of rows of data on a single page.

Ruby logo on railroad tracks

The app used the standard fragment caching and Russian doll caching as described in the Rails documentation.

I needed to understand if the caching tricks I used from the documentation were working locally. As it turns out, it’s easier than you think.

In the following steps, I will describe how to know if your stuff is being cached, how to clear the cache to see how long a fresh request will take, and finally a few other tips I pickedup.

Step 1 – Look at the logs to know if your stuff is being cached

Firstly, before hitting the cache, Rails will have to render everything.

Consequently, you’ll see something like the following in your logs.

Rendered tapes/_shared_tape.html.erb (1.3ms)
Rendered tapes/_shared_tape.html.erb (1.2ms)
Rendered tapes/_shared_tape.html.erb (1.0ms)
Rendered collection of tapes/_tape.html.erb [0 / 1536 cache hits] (4634.2ms)

Once Rails finishes caching, you’ll see something like the below:

Cache digest for app/views/tapes/_shared_tape.html.erb: 5783abcdfdfdfdf
Read fragment tapes/_shared_tape.html/ab8fdfgdfdfdfdfdfdf (0.1ms)
Write fragment views/footer/ab8fdfgdfdfdfdfdfdf (0.1ms)

Step 2 – Clear your Rails cache locally if you want to see how long a fresh request takes

When developing locally, you’ll want to be able to clear the cached results from time to time for debugging purposes. Steps 2-3 will help you do that.

For example, I wanted to see how long a certain request was taking on the initial non-cached page load.

You can either clear the cache from the console or clear it with a rake task.

Clear it from the console

Boot up the rails console and do:

Rails.cache.clear

Clear it with a rake task

rake tmp:cache:clear

Step 3 – Reset Your Cache in Chrome Dev Tools Whether Or Not You Change the JavaScript Files

Next, you should reset your browser cache. This applies whether or not you are debugging JavaScript.

In order to do this in the Google Chrome browser, open developer tools.

After doing this, mouse click and hold on the refresh button.

Finally, select the “Empty Cache and Hard Reload” option. As explained in this post, this forces the browser to re-download everything.

If you are debugging JavaScript, I’ve run into the experience of thinking a JavaScript file I had just changed was being used when in fact a cached version of it was being used. Thus, I was not using the new JavaScript file. This is why I emphasize doing a hard reload.

Step 4 – Use production data (or a large size dataset) if you can

The effects of caching are easier to observe with a large dataset. In my case, I was reading thousands of rows. But if you don’t have a large dataset, you can use the logs.

A Bit About Memcached

Memcached is pretty easy to configure for Heroku.

In config/environments/production.rb:

# Use a different cache store in production.
  config.cache_store = :mem_cache_store,
    ENV["MEMCACHED_SERVERS"].split(','), {
      username: ENV["CLOUD_USERNAME"],
      password: ENV["CLOUD_PASSWORD"],
      value_max_bytes: 2000000
    }

Step 5 – A word about Rails 5 caching

If you created a Rails 5 application, you now have a new handy command to toggle caching on and off.

rails dev:cache

The rails dev:cache command enables caching in development mode. You can re-issue this command to disable caching in development mode.

From the issue on GitHub, here is how it works:

if Rails.root.join('tmp/caching-dev.txt').exist?
  config.action_controller.perform_caching = true
  config.static_cache_control = "public, max-age=172800"
  config.cache_store = :memory_store
else
  config.action_controller.perform_caching = false
  config.cache_store = :null_store
end

If a caching-dev file exists, then you turn on caching in development mode. The upshot is you no longer have to restart the server manually to turn caching on and off. Apparently, this feature is “not supported by unicorn, thin, and webrick” according to the blog post from BigBinary.

Summary

Testing to see if your cache is working locally comes down to a few basic steps.

Firstly, read the documentation and look at your local development log.

Finally, clear the Rails cache and hard reset the cache in Chrome Dev tools.