Expiring Cache Fragments In The Rails Console

One of the much larger Rails applications I work on makes heavy use of caching for obvious reasons. In some locations the cache isn’t auto-expiring at the moment. After doing a deployment I needed to expire these sections manually via the console for my first time. I grabbed the name of the fragment from the view where it was being cached.

- cache('all_category_judges') do

I only knew one way to access the cache in the console and attempted clearing the cache fragment from there.

$ rails c
> Rails.cache.delete('all_category_judges')
> nil

This didn’t work. After some reading I found out that Fragment caching is stored in a slightly different way and needs to be accessed via the controller.

Given the time I’ll do some more investigation on why the caching is handled differently.

> ActionController::Base.new.expire_fragment('all_category_judges')
> true

This gave me access to the fragement and allowed me to clear it.