Sweeping 清理cache:
  1. class ListsController < ApplicationController
  2. caches_action :index, :show, :public, :feed
  3. cache_sweeper :list_sweeper, :only => [ :edit, :destroy, :share ]
  4. end

可以单独定义:

  1. class ListSweeper < ActionController::Caching::Sweeper
  2. observe List, Item
  3. def after_lists_controller_update
  4. #clear cache
  5. end
  6. def after_save(record)
  7. list = record.is_a?(List) ? record : record.list
  8. expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id)
  9. expire_action(:controller => "lists", :action => "all")
  10. list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
  11. end
  12. end
也可以定义在Module里:
  1. class ListsController < ApplicationController
  2. caches_action :index, :show, :public, :feed
  3. cache_sweeper OpenBar::Sweeper, :only => [ :edit, :destroy, :share ]
  4. end
实现:
  1. module Sweeping
  2. def self.included(base) #:nodoc:
  3. base.extend(ClassMethods)
  4. end
  5. module ClassMethods #:nodoc:
  6. def cache_sweeper(*sweepers)
  7. configuration = sweepers.extract_options!
  8. sweepers.each do |sweeper|
  9. ActiveRecord::Base.observers << sweeper if defined?(ActiveRecord) and defined?(ActiveRecord::Base) # 增加一个监听器
  10. sweeper_instance = (sweeper.is_a?(Symbol) ? Object.const_get(sweeper.to_s.classify) : sweeper).instance # 根据sweeper名字定义一个实例
  11. if sweeper_instance.is_a?(Sweeper) # 增加为filter
  12. around_filter(sweeper_instance, :only => configuration[:only])
  13. else
  14. after_filter(sweeper_instance, :only => configuration[:only])
  15. end
  16. end
  17. end
  18. end
  19. end

关键看Sweeper的实现:

  1. if defined?(ActiveRecord) and defined?(ActiveRecord::Observer)
  2. class Sweeper < ActiveRecord::Observer #:nodoc:
  3. attr_accessor :controller
  4. def before(controller)
  5. self.controller = controller
  6. callback(:before) if controller.perform_caching
  7. end
  8. def after(controller)
  9. callback(:after) if controller.perform_caching
  10. # Clean up, so that the controller can be collected after this request
  11. self.controller = nil
  12. end
  13. protected
  14. # gets the action cache path for the given options.
  15. def action_path_for(options)
  16. ActionController::Caching::Actions::ActionCachePath.path_for(controller, options)
  17. end
  18. # Retrieve instance variables set in the controller.
  19. def assigns(key)
  20. controller.instance_variable_get("@#{key}")
  21. end
  22. private
  23. def callback(timing)
  24. # 生成 controller 回调方法的名字,以及当前正在调用的 action 的名字
  25. controller_callback_method_name = "#{timing}_#{controller.controller_name.underscore}"
  26. action_callback_method_name     = "#{controller_callback_method_name}_#{controller.action_name}"
  27. # 如果有这些方法那么调用
  28. send!(controller_callback_method_name) if respond_to?(controller_callback_method_name, true)
  29. send!(action_callback_method_name)     if respond_to?(action_callback_method_name, true)
  30. end
  31. def method_missing(method, *arguments)
  32. return if @controller.nil?
  33. @controller.send!(method, *arguments)
  34. end
  35. end
  36. end

Rails源代码分析(22):ActionController::Caching(6) Sweeping相关推荐

  1. Fabric 1.0源代码分析(22)Ledger #blkstorage(block文件存储)

    # Fabric 1.0源代码笔记 之 Ledger #blkstorage(block文件存储) ## blkstorage概述 blkstorage,默认目录/var/hyperledger/pr ...

  2. Zepto源代码分析之二~三个API

    因为时间关系:本次仅仅对这三个API($.camelCase.$.contains.$.each)方法进行分析 第一个方法变量转驼峰:$.camelCase('hello-world-welcome' ...

  3. RTMPdump(libRTMP) 源代码分析 9: 接收消息(Message)(接收视音频数据)

    2019独角兽企业重金招聘Python工程师标准>>> 注:此前写了一些列的分析RTMPdump(libRTMP)源代码的文章,在此列一个列表: RTMPdump 源代码分析 1: ...

  4. 新秀nginx源代码分析数据结构篇(四)红黑树ngx_rbtree_t

    新秀nginx源代码分析数据结构篇(四)红黑树ngx_rbtree_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...

  5. Android系统进程间通信(IPC)机制Binder中的Server启动过程源代码分析

    原文地址: http://blog.csdn.net/luoshengyang/article/details/6629298 在前面一篇文章浅谈Android系统进程间通信(IPC)机制Binder ...

  6. RTMPdump(libRTMP) 源代码分析 7: 建立一个流媒体连接 (NetStream部分 2)

    ===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...

  7. Android应用Activity、Dialog、PopWindow、Toast窗体加入机制及源代码分析

    [工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重劳动成果] 1 背景 之所以写这一篇博客的原因是由于之前有写过一篇<Android应用setCont ...

  8. Android应用程序绑定服务(bindService)的过程源代码分析

    Android应用程序组件Service与Activity一样,既可以在新的进程中启动,也可以在应用程序进程内部启动:前面我们已经分析了在新的进程中启动Service的过程,本文将要介绍在应用程序内部 ...

  9. 变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)

    首先回顾前面的文章,我们把for_each 归类为非变动性算法,实际上它也可以算是变动性算法,取决于传入的第三个参数,即函数 指针.如果在函数内对容器元素做了修改,那么就属于变动性算法. 变动性算法源 ...

最新文章

  1. 浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路(1)
  2. 修改Eclipse自动换行长度
  3. c语言赋值运算符左边必须是,C语言运算符入门讲解
  4. hbase 核心知识
  5. asp.net程序中最常用的三十三种编程代码(转自CSDN)
  6. 华为配置DHCPv6
  7. okhttp实现连接池原理
  8. 在这做一个词云图生成器来送给大家(附代码),建议收藏
  9. 【Python】过滤表情字符
  10. 酒水知识(六大基酒之朗姆酒_Rum)
  11. 什么鬼畜耳机品牌会叫做233621
  12. 慢腾腾的Quartus prime16.0加快编译速度
  13. 我的学习笔记002--asp.net中的路径mxx
  14. [book]《心流:最优体验心理学》
  15. Wannafly挑战赛24 D 无限手套
  16. 牛客小白月赛25 D.抽卡
  17. 台式计算机虚拟内存怎么设置,电脑虚拟内存怎么设置
  18. 从新生宿舍到浙江大学计算机学院,2020年浙江大学新生宿舍环境条件,大一新生男生女生宿舍内部图片【多图】...
  19. ps2模拟器bios3dm_呆萌ps2模拟器bios文件
  20. 录屏软件Kap使用经验分享

热门文章

  1. JAVA获取机器的MAC地址
  2. 创建茶农茶企双赢平台——茶叶专业合作社
  3. navicat汉化版安装包下载
  4. java判断优先级代码_java运算符优先级排序正确的是哪些?
  5. c语言复数除法结构体,试用C语言的结构类型定义表示复数的抽象数据类型。
  6. Android 刷机常用命令
  7. iOS开发教程之OC语言-欧阳坚-专题视频课程
  8. 图片工厂怎么制作场景拼图 图片工厂制作场景拼图教程
  9. Oracle数据库表的基本操作以及空值问题的解决
  10. java图片强绘制表情符号_java - 具有表情符号的Graphics2D.drawString()无法正常工作 - 堆栈内存溢出...