Expiring Cache on Rails Using Jobs

I needed to expire caches using jobs. If I used an observer or a sweeper the updates would happen to often. I was displaying the info of a bunch of records (foobars) on a single view and I didn’t want it to update every time a Foobar was created – that would be too often and reduce the benefits of caching.

Instead I wanted a job that ran every hour to expire the cache.

First, I installed the Whenever gem which updates the cron file and helping you schedule server jobs.

use the command `wheneverize .` to generate the schedule.rb file for Whenever