Filtering sweeper calls in Rails
I use Devise for authentication on a model that heavily relies on cachin开发者_StackOverflowg. Because of update statements on both sign in and sign out, the cache sweeper for this model gets called on every sign in/out.
Is there a way to filter the origin of the sweeper callback?
I tried skipping the sweeper like this:
class ModelSweeper < ActionController::Caching::Sweeper
def after_update(model)
unless model.current_sign_in_at_changed? or model.last_sign_in_at_changed?
expire_cache_for(model)
end
end
private
def expire_cache_for(model)
#some expire cache code
end
end
The current_sign_in_at and last_sign_in_at are the two fields that are updated by devise during sign_in and sign_out. This code makes the obvious assumption that you have no application logic of your own to update these fields and only devise updates them.
精彩评论