2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-23 17:55:49 -05:00
|
|
|
module AbstractController
|
2015-12-30 08:07:40 -05:00
|
|
|
module Caching
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
extend ActiveSupport::Autoload
|
|
|
|
|
2016-02-23 17:55:49 -05:00
|
|
|
eager_autoload do
|
|
|
|
autoload :Fragments
|
|
|
|
end
|
|
|
|
|
2015-12-30 08:07:40 -05:00
|
|
|
module ConfigMethods
|
|
|
|
def cache_store
|
|
|
|
config.cache_store
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_store=(store)
|
2020-02-05 00:31:11 -05:00
|
|
|
config.cache_store = ActiveSupport::Cache.lookup_store(*store)
|
2015-12-30 08:07:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def cache_configured?
|
|
|
|
perform_caching && cache_store
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include ConfigMethods
|
2016-02-23 17:55:49 -05:00
|
|
|
include AbstractController::Caching::Fragments
|
2015-12-30 08:07:40 -05:00
|
|
|
|
|
|
|
included do
|
|
|
|
extend ConfigMethods
|
|
|
|
|
|
|
|
config_accessor :default_static_extension
|
2016-08-06 12:51:43 -04:00
|
|
|
self.default_static_extension ||= ".html"
|
2015-12-30 08:07:40 -05:00
|
|
|
|
|
|
|
config_accessor :perform_caching
|
|
|
|
self.perform_caching = true if perform_caching.nil?
|
|
|
|
|
2016-07-14 06:38:16 -04:00
|
|
|
config_accessor :enable_fragment_cache_logging
|
|
|
|
self.enable_fragment_cache_logging = false
|
|
|
|
|
2017-05-29 12:01:50 -04:00
|
|
|
class_attribute :_view_cache_dependencies, default: []
|
2015-12-30 08:07:40 -05:00
|
|
|
helper_method :view_cache_dependencies if respond_to?(:helper_method)
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def view_cache_dependency(&dependency)
|
|
|
|
self._view_cache_dependencies += [dependency]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def view_cache_dependencies
|
|
|
|
self.class._view_cache_dependencies.map { |dep| instance_exec(&dep) }.compact
|
|
|
|
end
|
|
|
|
|
2016-12-22 04:48:24 -05:00
|
|
|
private
|
2015-12-30 08:07:40 -05:00
|
|
|
# Convenience accessor.
|
2016-12-22 04:48:24 -05:00
|
|
|
def cache(key, options = {}, &block) # :doc:
|
2016-01-06 02:29:22 -05:00
|
|
|
if cache_configured?
|
|
|
|
cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block)
|
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
2015-12-30 08:07:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|