Expire cache item in Rails ActiveSupport::Cache::FileStore

Wouldn’t it be nice to cache some item (e.g. action) for some amount of time, before expiring it? For example, if you’re displaying some page that parts of it change very often, but you don’t want to expire cache every time, since you prefer to serve stale information instead of expiring the page?
The answer is the options[:expires_in] which is supported by the Rails’ ActiveSupport::Cache::MemCacheStore.
The current implementation of ActiveSupport::Cache::FileStore, however, lacks such feature, despite the fact that it’s really easy to implement.

This is the fix accompanied by a unit-test.

And then you can happily add a line like this in your controllers:

caches_action :find, :expires_in => 10.minute

The check, by the way, adds about 7usec to cache read request, which IMHO is negligible.

The Rails core team doesn’t seem to be interested in this patch, though.

Leave a Reply