02 12 / 2011
Cache sweeper in Rails3.1, if not using ActiveRecord!
If you’re using Rails 3.1 but without ActiveRecord, then you cannot use the the expire_page, expire_action and expire_fragment in your controller. This might happen if you use ODMs such as Mongoid. The app that I’m working on uses Mongoid.
So, the work-around is to add a Mongoid::Observer and expire those caches by creating an instance of the ActionController::Base and call those methods on that object.
class LineObserver < Mongoid::Observer
observe :line
def after_save(line)
expire_cache(line)
end
def after_destroy(line)
expire_cache(line)
end
private
def expire_cache(line)
@cont ||= ActionController::Base.new
@cont.expire_fragment "department_lines" if line.public?
end
end
Hope this saves someone’s time.
Permalink 9 notes