Rails源代码分析(17):ActionController::Caching(1)
1 框架Cache可以很方便的来加速应用,可以在计算、rendering以及数据库调用ActionController有3种方式的cache:Page, Action, FragmentTo turn off all caching and sweeping, set Base.perform_caching = false.Configuration examples (MemoryStore
·
1 框架
Cache可以很方便的来加速应用,可以在计算、rendering以及数据库调用
ActionController有3种方式的cache:
Page, Action, Fragment
To turn off all caching and sweeping, set Base.perform_caching = false.
Configuration examples (MemoryStore is the default):
# ActionController::Base.cache_store = :memory_store
# ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
# ActionController::Base.cache_store = :drb_store, "druby://localhost:9192"
# ActionController::Base.cache_store = :mem_cache_store, "localhost"
# ActionController::Base.cache_store = MyOwnStore.new("parameter")
- module Caching
- def self.included(base) #:nodoc:
- base.class_eval do
- @@cache_store = nil
- cattr_reader :cache_store
- # Defines the storage option for cached fragments
- #
- def self.cache_store=(store_option)
- @@cache_store = ActiveSupport::Cache.lookup_store(store_option)
- end
- include Pages, Actions, Fragments
- include Sweeping, SqlCache if defined?(ActiveRecord)
- @@perform_caching = true
- cattr_accessor :perform_caching
- # 是否配置了cache,取决于 perform_caching 和 cache_store 设置了
- def self.cache_configured?
- perform_caching && cache_store
- end
- end
- end
- protected
- # Convenience accessor
- def cache(key, options = {}, &block)
- if cache_configured?
- cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block)
- else
- yield
- end
- end
- private
- def cache_configured?
- self.class.cache_configured?
- end
- end
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献6条内容
所有评论(0)