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")

  1.   module Caching
  2.     def self.included(base) #:nodoc:
  3.       base.class_eval do
  4.         @@cache_store = nil
  5.         cattr_reader :cache_store
  6.         # Defines the storage option for cached fragments
  7.         # 
  8.         def self.cache_store=(store_option)
  9.           @@cache_store = ActiveSupport::Cache.lookup_store(store_option)
  10.         end
  11.         include Pages, Actions, Fragments
  12.         include Sweeping, SqlCache if defined?(ActiveRecord)
  13.         @@perform_caching = true
  14.         cattr_accessor :perform_caching
  15.         # 是否配置了cache,取决于 perform_caching 和 cache_store 设置了
  16.        def self.cache_configured?
  17.           perform_caching && cache_store
  18.         end
  19.       end
  20.     end
  21.     protected
  22.       # Convenience accessor
  23.       def cache(key, options = {}, &block)
  24.         if cache_configured?
  25.           cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block)
  26.         else
  27.           yield
  28.         end
  29.       end
  30.     private    
  31.       def cache_configured?
  32.         self.class.cache_configured?
  33.       end
  34.   end
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐