Rails 3: invalidating a cache outside of a controller
I have certain pieces of data that are updated infrequently - say, once every few weeks or so. Additionally, they're shared amongst users, and can take some time to render as JSON. This makes them ideal to cache. So, I cache their rendered output via caches_action
.
Soon, I'll be updating this data from a Resque job, and after updating it successfully, I will need to invalidate the cache. I'm not sure where to do this, as it seems like the job of the controller. It doesn't seem right to put it in the model, as its more of a presentation layer concern. (After all, why should the model care that JSON output takes forever?)
I don't think a sweeper would work here, as it operates within a controller, correct? I've seen people suggest instantiating the controller in question within the job, but that really isn't nice e开发者_如何学Pythonither. Has anyone dealt with this in a DRY-ish way? The only way I see to do it is to manipulate Rails.cache
manually.
Would an observer work for you? You can setup observers to watch for changes to records then do something after it changes, but it mainly just reduces clutter in the model. You'd have to use Rail.cache in either place. An observer just cleans that code out of the model.
Check out:
http://www.daokaous.com/rails3.0.0_doc/classes/ActiveModel/Observer.html
精彩评论