Forum: Ruby on Rails Expire_fragment in observer/model? Works fine in Sweepers.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Marston A. (Guest)
on 2008-12-15 20:09
(Received via mailing list)
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.
Steven C. (Guest)
on 2010-09-08 17:20
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_fragm...
Ar C. (Guest)
on 2010-09-08 18:30
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_fragm...

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.
This topic is locked and can not be replied to.