Rails源代码分析(22):ActionController::Caching(6) Sweeping
- class ListsController < ApplicationController
- caches_action :index, :show, :public, :feed
- cache_sweeper :list_sweeper, :only => [ :edit, :destroy, :share ]
- end
可以单独定义:
- class ListSweeper < ActionController::Caching::Sweeper
- observe List, Item
- def after_lists_controller_update
- #clear cache
- end
- def after_save(record)
- list = record.is_a?(List) ? record : record.list
- expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id)
- expire_action(:controller => "lists", :action => "all")
- list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
- end
- end
- class ListsController < ApplicationController
- caches_action :index, :show, :public, :feed
- cache_sweeper OpenBar::Sweeper, :only => [ :edit, :destroy, :share ]
- end
- module Sweeping
- def self.included(base) #:nodoc:
- base.extend(ClassMethods)
- end
- module ClassMethods #:nodoc:
- def cache_sweeper(*sweepers)
- configuration = sweepers.extract_options!
- sweepers.each do |sweeper|
- ActiveRecord::Base.observers << sweeper if defined?(ActiveRecord) and defined?(ActiveRecord::Base) # 增加一个监听器
- sweeper_instance = (sweeper.is_a?(Symbol) ? Object.const_get(sweeper.to_s.classify) : sweeper).instance # 根据sweeper名字定义一个实例
- if sweeper_instance.is_a?(Sweeper) # 增加为filter
- around_filter(sweeper_instance, :only => configuration[:only])
- else
- after_filter(sweeper_instance, :only => configuration[:only])
- end
- end
- end
- end
- end
关键看Sweeper的实现:
- if defined?(ActiveRecord) and defined?(ActiveRecord::Observer)
- class Sweeper < ActiveRecord::Observer #:nodoc:
- attr_accessor :controller
- def before(controller)
- self.controller = controller
- callback(:before) if controller.perform_caching
- end
- def after(controller)
- callback(:after) if controller.perform_caching
- # Clean up, so that the controller can be collected after this request
- self.controller = nil
- end
- protected
- # gets the action cache path for the given options.
- def action_path_for(options)
- ActionController::Caching::Actions::ActionCachePath.path_for(controller, options)
- end
- # Retrieve instance variables set in the controller.
- def assigns(key)
- controller.instance_variable_get("@#{key}")
- end
- private
- def callback(timing)
- # 生成 controller 回调方法的名字,以及当前正在调用的 action 的名字
- controller_callback_method_name = "#{timing}_#{controller.controller_name.underscore}"
- action_callback_method_name = "#{controller_callback_method_name}_#{controller.action_name}"
- # 如果有这些方法那么调用
- send!(controller_callback_method_name) if respond_to?(controller_callback_method_name, true)
- send!(action_callback_method_name) if respond_to?(action_callback_method_name, true)
- end
- def method_missing(method, *arguments)
- return if @controller.nil?
- @controller.send!(method, *arguments)
- end
- end
- end
Rails源代码分析(22):ActionController::Caching(6) Sweeping相关推荐
- Fabric 1.0源代码分析(22)Ledger #blkstorage(block文件存储)
# Fabric 1.0源代码笔记 之 Ledger #blkstorage(block文件存储) ## blkstorage概述 blkstorage,默认目录/var/hyperledger/pr ...
- Zepto源代码分析之二~三个API
因为时间关系:本次仅仅对这三个API($.camelCase.$.contains.$.each)方法进行分析 第一个方法变量转驼峰:$.camelCase('hello-world-welcome' ...
- RTMPdump(libRTMP) 源代码分析 9: 接收消息(Message)(接收视音频数据)
2019独角兽企业重金招聘Python工程师标准>>> 注:此前写了一些列的分析RTMPdump(libRTMP)源代码的文章,在此列一个列表: RTMPdump 源代码分析 1: ...
- 新秀nginx源代码分析数据结构篇(四)红黑树ngx_rbtree_t
新秀nginx源代码分析数据结构篇(四)红黑树ngx_rbtree_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...
- Android系统进程间通信(IPC)机制Binder中的Server启动过程源代码分析
原文地址: http://blog.csdn.net/luoshengyang/article/details/6629298 在前面一篇文章浅谈Android系统进程间通信(IPC)机制Binder ...
- RTMPdump(libRTMP) 源代码分析 7: 建立一个流媒体连接 (NetStream部分 2)
===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...
- Android应用Activity、Dialog、PopWindow、Toast窗体加入机制及源代码分析
[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重劳动成果] 1 背景 之所以写这一篇博客的原因是由于之前有写过一篇<Android应用setCont ...
- Android应用程序绑定服务(bindService)的过程源代码分析
Android应用程序组件Service与Activity一样,既可以在新的进程中启动,也可以在应用程序进程内部启动:前面我们已经分析了在新的进程中启动Service的过程,本文将要介绍在应用程序内部 ...
- 变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)
首先回顾前面的文章,我们把for_each 归类为非变动性算法,实际上它也可以算是变动性算法,取决于传入的第三个参数,即函数 指针.如果在函数内对容器元素做了修改,那么就属于变动性算法. 变动性算法源 ...
最新文章
- 浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路(1)
- 修改Eclipse自动换行长度
- c语言赋值运算符左边必须是,C语言运算符入门讲解
- hbase 核心知识
- asp.net程序中最常用的三十三种编程代码(转自CSDN)
- 华为配置DHCPv6
- okhttp实现连接池原理
- 在这做一个词云图生成器来送给大家(附代码),建议收藏
- 【Python】过滤表情字符
- 酒水知识(六大基酒之朗姆酒_Rum)
- 什么鬼畜耳机品牌会叫做233621
- 慢腾腾的Quartus prime16.0加快编译速度
- 我的学习笔记002--asp.net中的路径mxx
- [book]《心流:最优体验心理学》
- Wannafly挑战赛24 D 无限手套
- 牛客小白月赛25 D.抽卡
- 台式计算机虚拟内存怎么设置,电脑虚拟内存怎么设置
- 从新生宿舍到浙江大学计算机学院,2020年浙江大学新生宿舍环境条件,大一新生男生女生宿舍内部图片【多图】...
- ps2模拟器bios3dm_呆萌ps2模拟器bios文件
- 录屏软件Kap使用经验分享
热门文章
- JAVA获取机器的MAC地址
- 创建茶农茶企双赢平台——茶叶专业合作社
- navicat汉化版安装包下载
- java判断优先级代码_java运算符优先级排序正确的是哪些?
- c语言复数除法结构体,试用C语言的结构类型定义表示复数的抽象数据类型。
- Android 刷机常用命令
- iOS开发教程之OC语言-欧阳坚-专题视频课程
- 图片工厂怎么制作场景拼图 图片工厂制作场景拼图教程
- Oracle数据库表的基本操作以及空值问题的解决
- java图片强绘制表情符号_java - 具有表情符号的Graphics2D.drawString()无法正常工作 - 堆栈内存溢出...