Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call expire_fragment from Rails Observer/Model?

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.

like image 800
Marston A. Avatar asked Dec 26 '08 02:12

Marston A.


2 Answers

Disclaimer: My rails is a bit rusty, but this or something like it should work

ActionController::Base.new.expire_fragment(key, options = nil) 
like image 164
Orion Edwards Avatar answered Oct 24 '22 12:10

Orion Edwards


The solution provided by Orion works perfectly. As an enhancement and for convenience, I've put the following code into config/initializers/active_record_expire_fragment.rb

class ActiveRecord::Base
  def expire_fragment(*args)
    ActionController::Base.new.expire_fragment(*args)
  end
end

Now, you can use expire_fragment on all instances of ActiveRecord::Base, e.g. User.first.expire_fragment('user-stats')

like image 13
TheDeadSerious Avatar answered Oct 24 '22 11:10

TheDeadSerious