caching.rb 2.8 KB
Newer Older
1
require 'fileutils'
2
require 'uri'
3
require 'set'
4

5
module ActionController
6
  # \Caching is a cheap way of speeding up slow applications by keeping the result of
Y
Yehuda Katz 已提交
7
  # calculations, renderings, and database calls around for subsequent requests.
8
  #
Y
Yehuda Katz 已提交
9 10
  # You can read more about each approach and the sweeping assistance by clicking the
  # modules below.
11
  #
Y
Yehuda Katz 已提交
12 13
  # Note: To turn off all caching and sweeping, set
  #   config.action_controller.perform_caching = false.
14
  #
15
  # == \Caching stores
16
  #
Y
Yehuda Katz 已提交
17
  # All the caching stores from ActiveSupport::Cache are available to be used as backends
18
  # for Action Controller caching.
19 20 21
  #
  # Configuration examples (MemoryStore is the default):
  #
Y
Yehuda Katz 已提交
22
  #   config.action_controller.cache_store = :memory_store
23 24 25 26
  #   config.action_controller.cache_store = :file_store, '/path/to/cache/directory'
  #   config.action_controller.cache_store = :mem_cache_store, 'localhost'
  #   config.action_controller.cache_store = :mem_cache_store, Memcached::Rails.new('localhost:11211')
  #   config.action_controller.cache_store = MyOwnStore.new('parameter')
27
  module Caching
28
    extend ActiveSupport::Concern
C
Carlhuda 已提交
29
    extend ActiveSupport::Autoload
30

J
Joshua Peek 已提交
31 32 33 34 35
    eager_autoload do
      autoload :Fragments
      autoload :Sweeper, 'action_controller/caching/sweeping'
      autoload :Sweeping, 'action_controller/caching/sweeping'
    end
36

37 38 39 40
    module ConfigMethods
      def cache_store
        config.cache_store
      end
41

42 43
      def cache_store=(store)
        config.cache_store = ActiveSupport::Cache.lookup_store(store)
44
      end
45

D
David Heinemeier Hansson 已提交
46 47 48 49
      private
        def cache_configured?
          perform_caching && cache_store
        end
50 51
    end

52 53 54
    include RackDelegation
    include AbstractController::Callbacks

55
    include ConfigMethods
56
    include Fragments
C
Carlhuda 已提交
57
    include Sweeping if defined?(ActiveRecord)
58 59 60 61

    included do
      extend ConfigMethods

62 63 64 65 66 67 68 69 70 71 72 73
      config_accessor :default_static_extension
      self.default_static_extension ||= '.html'

      def self.page_cache_extension=(extension)
        ActiveSupport::Deprecation.deprecation_warning(:page_cache_extension, :default_static_extension)
        self.default_static_extension = extension
      end

      def self.page_cache_extension
        ActiveSupport::Deprecation.deprecation_warning(:page_cache_extension, :default_static_extension)
        default_static_extension
      end
74

75 76
      config_accessor :perform_caching
      self.perform_caching = true if perform_caching.nil?
Y
Yehuda Katz 已提交
77
    end
78

79 80 81
    def caching_allowed?
      request.get? && response.status == 200
    end
82

D
David Heinemeier Hansson 已提交
83
    protected
84
      # Convenience accessor.
D
David Heinemeier Hansson 已提交
85 86 87 88 89 90
      def cache(key, options = {}, &block)
        if cache_configured?
          cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block)
        else
          yield
        end
91
      end
92
  end
93
end