blog_sweeper.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:2k
源码类别:

Ajax

开发平台:

Others

  1. class BlogSweeper < ActionController::Caching::Sweeper
  2.   observe Category, Blog, User, Article, Page, Categorization, Comment, Trackback
  3.   def pending_sweeps
  4.     @pending_sweeps ||= Set.new
  5.   end
  6.   def run_pending_page_sweeps
  7.     pending_sweeps.each do |each|
  8.       self.send(each)
  9.     end
  10.   end
  11.   def after_comments_create
  12.     expire_for(controller.send(:instance_variable_get, :@comment))
  13.   end
  14.   alias_method :after_comments_update, :after_comments_create
  15.   alias_method :after_articles_comment, :after_comments_create
  16.   def after_comments_destroy
  17.     expire_for(controller.send(:instance_variable_get, :@comment), true)
  18.   end
  19.   alias_method :after_articles_nuke_comment, :after_comments_destroy
  20.   def after_articles_trackback
  21.     expire_for(controller.send(:instance_variable_get, :@trackback))
  22.   end
  23.   def after_articles_nuke_trackback
  24.     expire_for(controller.send(:instance_variable_get, :@trackback), true)
  25.   end
  26.   def after_save(record)
  27.     expire_for(record)
  28.   end
  29.   def after_destroy(record)
  30.     expire_for(record, true)
  31.   end
  32.   def expire_for(record, destroying = false)
  33.     case record
  34.     when Page
  35.       pending_sweeps << :sweep_pages
  36.     when Content
  37.       if record.invalidates_cache?(destroying)
  38.         pending_sweeps << :sweep_articles << :sweep_pages
  39.       end
  40.     when Category, Categorization
  41.       pending_sweeps << :sweep_articles << :sweep_pages
  42.     when Blog, User, Comment, Trackback
  43.       pending_sweeps << :sweep_all << :sweep_theme
  44.     end
  45.     unless controller
  46.       run_pending_page_sweeps
  47.     end
  48.   end
  49.   def sweep_all
  50.     PageCache.sweep_all
  51.   end
  52.   def sweep_theme
  53.     PageCache.sweep_theme_cache
  54.   end
  55.   def sweep_articles
  56.     PageCache.sweep_all
  57.   end
  58.   def sweep_pages
  59.     PageCache.zap_pages('pages') unless Blog.default.nil?
  60.   end
  61.   def logger
  62.     @logger ||= RAILS_DEFAULT_LOGGER || Logger.new(STDERR)
  63.   end
  64.   private
  65.   def callback(timing)
  66.     super
  67.     if timing == :after
  68.       run_pending_page_sweeps
  69.     end
  70.   end
  71. end