开发者

Filtering sweeper calls in Rails

开发者 https://www.devze.com 2023-03-13 19:24 出处:网络
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 o

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消