Expire_fragment in observer/model? Works fine in Sweepers.

I've pretty much tried everything, but it seems impossible to use expire_fragment from models? I know you're not supposed to and it's non-MVC, but surely there much be some way to do it.

I created a module in /lib/cache_helper.rb with all my expire helpers, within each are just a bunch of expire_fragment calls. I have all my cache sweepers setup under /app/sweepers and have an "include CacheHelper" in my application controller so expiring cache within the app when called via controllers works fine.

Then things is I have some external daemons and especially some recurring cron tasks which call a rake task that calls a certain method. This method does some processing and inputs entries into the model, after which I need to expire cache. What's the best way to do this as I can't specify "cache_sweeper" within the model.

Straight up observers seem to be the best solution but then it complains about expire_fragment being undefined etc etc, I've even tried including the ActionController caching classes into the observer but that didn't work. I'd love some ideas of how to create a solution for this. Thanks.

Marston A. wrote:

I've pretty much tried everything, but it seems impossible to use expire_fragment from models? I know you're not supposed to and it's non-MVC, but surely there much be some way to do it.

I created a module in /lib/cache_helper.rb with all my expire helpers, within each are just a bunch of expire_fragment calls. I have all my cache sweepers setup under /app/sweepers and have an "include CacheHelper" in my application controller so expiring cache within the app when called via controllers works fine.

Then things is I have some external daemons and especially some recurring cron tasks which call a rake task that calls a certain method. This method does some processing and inputs entries into the model, after which I need to expire cache. What's the best way to do this as I can't specify "cache_sweeper" within the model.

Straight up observers seem to be the best solution but then it complains about expire_fragment being undefined etc etc, I've even tried including the ActionController caching classes into the observer but that didn't work. I'd love some ideas of how to create a solution for this. Thanks.

Had this problem too and wrote up the solution the other night on my blog.

http://stevencummings.name/2010/09/07/expire_fragment-with-rake-and-as-a-model-only-observer

Steven Cummings wrote:

Had this problem too and wrote up the solution the other night on my blog.

http://stevencummings.name/2010/09/07/expire_fragment-with-rake-and-as-a-model-only-observer

Interesting solution...

Related to model-based cache expiration, in one large-ish Rails project, the sidebar for a 'show' of an entity contains a view of all the related entities in the application. We were getting hammered on both db access and rendering time, so I implemented fragment caching in a multi-tiered approach, and left it to the models to expire caches of their data -- a fine line to walk, but that worked the best for us.

We used a Rails.cache.delete() call from within the model to handle the fragment expiration, with the new/edited/deleted model selecting the appropriate caches to expire according to its related models.