How do you clear cached actions in Rails?
Careful, this is a trick question!
The obvious thing is to call ‘expire_action’ to remove the cached entries for the specified action. However, the fragment cache path for an action includes the controller name, and the expire_action method is a method on the controller, so the only cached actions you can expire are ones on the current controller.
What this really means is that those of you with ‘Admin’ controllers have to pass the controller into the expire_action as part of the options hash. This is essentially the same as url_for(), because url_for() is the method used to build the fragment cache key.
Seems like this can be improved.
And an opportunity for my action_cache plugin to help out. More later.
Update: Changed some information, as I found how the action cache uses url_for.
I haven’t actually expired an action for another controller yet, but I have expired other actions before. For example I have an after_filter for update and destroy that does something like this:
Since expire_action (partly) relies on url_for to generate the fragment cache path, wouldn’t this work if you needed to clear a cached Admin action:
From my experience url_for will generate a URL relative to current controller and action, but you can supply extra arguments to reference any other controller’s action.
Yup, you’re right. I updated my post as I understood the code further. It’s still kind of messy. I don’t think you can pass in, for instance, a named route, but I may be wrong.
I guess I’ll be playing around with this to find out how this works, and make expiry work with my action_cache plugin.