2015-11-02 21:30:50 -05:00
|
|
|
require 'concurrent/map'
|
2012-12-16 17:17:53 -05:00
|
|
|
require 'action_view/dependency_tracker'
|
2013-07-30 09:17:27 -04:00
|
|
|
require 'monitor'
|
2012-10-17 18:20:51 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
module ActionView
|
|
|
|
class Digestor
|
2012-09-05 13:38:16 -04:00
|
|
|
cattr_reader(:cache)
|
2015-09-19 09:56:26 -04:00
|
|
|
@@cache = Concurrent::Map.new
|
2013-07-30 09:17:27 -04:00
|
|
|
@@digest_monitor = Monitor.new
|
|
|
|
|
2015-05-30 17:11:49 -04:00
|
|
|
class PerRequestDigestCacheExpiry < Struct.new(:app) # :nodoc:
|
|
|
|
def call(env)
|
|
|
|
ActionView::Digestor.cache.clear
|
|
|
|
app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-30 09:17:27 -04:00
|
|
|
class << self
|
2014-03-01 12:59:56 -05:00
|
|
|
# Supported options:
|
|
|
|
#
|
|
|
|
# * <tt>name</tt> - Template name
|
2015-06-14 00:35:34 -04:00
|
|
|
# * <tt>finder</tt> - An instance of <tt>ActionView::LookupContext</tt>
|
2014-03-01 12:59:56 -05:00
|
|
|
# * <tt>dependencies</tt> - An array of dependent views
|
|
|
|
# * <tt>partial</tt> - Specifies whether the template is a partial
|
2016-02-12 17:30:13 -05:00
|
|
|
def digest(name:, finder:, **options)
|
|
|
|
options.assert_valid_keys(:dependencies, :partial)
|
2014-03-01 12:59:56 -05:00
|
|
|
|
2016-02-12 17:30:13 -05:00
|
|
|
cache_key = ([ name, finder.details_key.hash ].compact + Array.wrap(options[:dependencies])).join('.')
|
2013-07-15 09:59:18 -04:00
|
|
|
|
2013-07-30 09:17:27 -04:00
|
|
|
# this is a correctly done double-checked locking idiom
|
2015-09-19 09:56:26 -04:00
|
|
|
# (Concurrent::Map's lookups have volatile semantics)
|
2013-07-30 09:17:27 -04:00
|
|
|
@@cache[cache_key] || @@digest_monitor.synchronize do
|
|
|
|
@@cache.fetch(cache_key) do # re-check under lock
|
2016-02-12 17:30:13 -05:00
|
|
|
compute_and_store_digest(cache_key, name, finder, options)
|
2013-07-30 09:17:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-07-07 12:44:16 -04:00
|
|
|
|
2013-07-30 09:17:27 -04:00
|
|
|
private
|
2016-02-12 17:30:13 -05:00
|
|
|
def compute_and_store_digest(cache_key, name, finder, options) # called under @@digest_monitor lock
|
|
|
|
klass = if options[:partial] || name.include?("/_")
|
2014-03-21 11:29:00 -04:00
|
|
|
# Prevent re-entry or else recursive templates will blow the stack.
|
|
|
|
# There is no need to worry about other threads seeing the +false+ value,
|
|
|
|
# as they will then have to wait for this thread to let go of the @@digest_monitor lock.
|
|
|
|
pre_stored = @@cache.put_if_absent(cache_key, false).nil? # put_if_absent returns nil on insertion
|
|
|
|
PartialDigestor
|
|
|
|
else
|
|
|
|
Digestor
|
|
|
|
end
|
2014-03-01 12:59:56 -05:00
|
|
|
|
2016-02-12 17:30:13 -05:00
|
|
|
@@cache[cache_key] = stored_digest = klass.new(name, finder, options).digest
|
2014-03-21 11:29:00 -04:00
|
|
|
ensure
|
|
|
|
# something went wrong or ActionView::Resolver.caching? is false, make sure not to corrupt the @@cache
|
|
|
|
@@cache.delete_pair(cache_key, false) if pre_stored && !stored_digest
|
2013-07-30 09:17:27 -04:00
|
|
|
end
|
2012-10-17 18:20:51 -04:00
|
|
|
end
|
|
|
|
|
2016-02-15 17:25:10 -05:00
|
|
|
def self.tree(name, finder, injected = [], partial = false, seen = {})
|
|
|
|
logical_name = name.gsub(%r|/_|, "/")
|
|
|
|
partial = partial || name.include?("/_")
|
|
|
|
|
|
|
|
if finder.disable_cache { finder.exists?(logical_name, [], partial) }
|
|
|
|
template = finder.disable_cache { finder.find(logical_name, [], partial) }
|
2016-02-12 20:21:22 -05:00
|
|
|
|
2016-02-15 17:25:10 -05:00
|
|
|
if obj = seen[template.identifier]
|
|
|
|
obj
|
2016-02-12 20:21:22 -05:00
|
|
|
else
|
2016-02-15 17:25:10 -05:00
|
|
|
klass = partial ? Partial : Node
|
|
|
|
node = seen[template.identifier] = klass.new(name, logical_name, template, partial, [])
|
2016-02-12 20:21:22 -05:00
|
|
|
|
2016-02-15 17:25:10 -05:00
|
|
|
deps = DependencyTracker.find_dependencies(name, template, finder.view_paths)
|
|
|
|
deps.each do |dep_file|
|
|
|
|
node.children << tree(dep_file, finder, [], true, seen)
|
|
|
|
end
|
|
|
|
injected.each do |template|
|
|
|
|
node.children << Injected.new(template, nil, nil, partial, [])
|
|
|
|
end
|
|
|
|
node
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
2016-02-15 17:25:10 -05:00
|
|
|
else
|
|
|
|
seen[name] = Missing.new(name, logical_name, nil, partial, [])
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Node < Struct.new(:name, :logical_name, :template, :partial, :children)
|
|
|
|
def to_dep(finder)
|
|
|
|
if partial
|
|
|
|
PartialDigestor.new(name, finder, partial: partial)
|
|
|
|
else
|
|
|
|
Digestor.new(name, finder, partial: partial)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-15 17:25:10 -05:00
|
|
|
def digest stack = []
|
|
|
|
Digest::MD5.hexdigest("#{template.source}-#{dependency_digest(stack)}")
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
|
|
|
|
2016-02-15 17:25:10 -05:00
|
|
|
def dependency_digest(stack)
|
|
|
|
children.map do |node|
|
|
|
|
if stack.include?(node)
|
|
|
|
false
|
|
|
|
else
|
|
|
|
stack.push node
|
|
|
|
node.digest(stack).tap { stack.pop }
|
|
|
|
end
|
|
|
|
end.join("-")
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-15 17:25:10 -05:00
|
|
|
class Partial < Node; end
|
|
|
|
|
2016-02-12 20:04:56 -05:00
|
|
|
class Missing < Node
|
2016-02-15 17:25:10 -05:00
|
|
|
def digest(_ = [])
|
2016-02-12 20:04:56 -05:00
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-15 17:25:10 -05:00
|
|
|
class Injected < Node
|
|
|
|
def digest(_ = [])
|
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-21 11:29:00 -04:00
|
|
|
attr_reader :name, :finder, :options
|
2014-03-01 12:59:56 -05:00
|
|
|
|
2016-02-12 17:30:13 -05:00
|
|
|
def initialize(name, finder, options = {})
|
2016-02-12 14:22:51 -05:00
|
|
|
@name, @finder = name, finder
|
|
|
|
@options = options
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def digest
|
2012-09-04 18:29:00 -04:00
|
|
|
Digest::MD5.hexdigest("#{source}-#{dependency_digest}").tap do |digest|
|
2016-02-11 14:20:20 -05:00
|
|
|
logger.debug " Cache digest for #{template.inspect}: #{digest}"
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
2016-02-11 15:44:22 -05:00
|
|
|
rescue ActionView::MissingTemplate
|
2016-02-11 14:20:20 -05:00
|
|
|
logger.error " Couldn't find template for digesting: #{name}"
|
2012-08-29 15:23:15 -04:00
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
def dependencies
|
2015-07-15 17:32:45 -04:00
|
|
|
DependencyTracker.find_dependencies(name, template, finder.view_paths)
|
2012-08-29 15:23:15 -04:00
|
|
|
rescue ActionView::MissingTemplate
|
2016-02-11 14:20:20 -05:00
|
|
|
logger.error " '#{name}' file doesn't exist, so no dependencies"
|
2015-05-07 13:17:45 -04:00
|
|
|
[]
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
2016-02-12 20:04:56 -05:00
|
|
|
def children
|
|
|
|
dependencies.collect do |dependency|
|
|
|
|
PartialDigestor.new(dependency, finder)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def nested_dependencies
|
|
|
|
dependencies.collect do |dependency|
|
2016-02-12 17:30:13 -05:00
|
|
|
dependencies = PartialDigestor.new(dependency, finder).nested_dependencies
|
2012-08-29 15:23:15 -04:00
|
|
|
dependencies.any? ? { dependency => dependencies } : dependency
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-02-11 14:52:21 -05:00
|
|
|
class NullLogger
|
|
|
|
def self.debug(_); end
|
|
|
|
def self.error(_); end
|
|
|
|
end
|
2016-02-11 14:20:20 -05:00
|
|
|
|
2012-09-05 12:51:24 -04:00
|
|
|
def logger
|
2016-02-11 14:20:20 -05:00
|
|
|
ActionView::Base.logger || NullLogger
|
2012-09-05 12:51:24 -04:00
|
|
|
end
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def logical_name
|
|
|
|
name.gsub(%r|/_|, "/")
|
|
|
|
end
|
2012-09-05 10:05:50 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def partial?
|
2012-10-17 18:20:51 -04:00
|
|
|
false
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
2012-12-16 17:17:53 -05:00
|
|
|
def template
|
2014-03-21 11:29:00 -04:00
|
|
|
@template ||= finder.disable_cache { finder.find(logical_name, [], partial?) }
|
2012-12-16 17:17:53 -05:00
|
|
|
end
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def source
|
2012-12-16 17:17:53 -05:00
|
|
|
template.source
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def dependency_digest
|
2013-01-08 13:20:47 -05:00
|
|
|
template_digests = dependencies.collect do |template_name|
|
2014-03-21 11:29:00 -04:00
|
|
|
Digestor.digest(name: template_name, finder: finder, partial: true)
|
2013-01-08 13:20:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
(template_digests + injected_dependencies).join("-")
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
2013-01-08 13:20:47 -05:00
|
|
|
def injected_dependencies
|
|
|
|
Array.wrap(options[:dependencies])
|
|
|
|
end
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
2012-10-17 18:20:51 -04:00
|
|
|
|
|
|
|
class PartialDigestor < Digestor # :nodoc:
|
|
|
|
def partial?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2012-09-05 10:05:50 -04:00
|
|
|
end
|