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
|
2016-02-16 18:21:16 -05:00
|
|
|
@@digest_mutex = Mutex.new
|
2013-07-30 09:17:27 -04:00
|
|
|
|
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-17 10:46:36 -05:00
|
|
|
def digest(name:, finder:, dependencies: [])
|
|
|
|
dependencies ||= []
|
2016-02-16 18:10:25 -05:00
|
|
|
cache_key = ([ name, finder.details_key.hash ].compact + 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)
|
2016-02-16 18:21:16 -05:00
|
|
|
@@cache[cache_key] || @@digest_mutex.synchronize do
|
2013-07-30 09:17:27 -04:00
|
|
|
@@cache.fetch(cache_key) do # re-check under lock
|
2016-02-16 18:10:25 -05:00
|
|
|
@@cache[cache_key] = tree(name, finder, dependencies).digest
|
2013-07-30 09:17:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-07-07 12:44:16 -04:00
|
|
|
|
2016-02-16 18:10:25 -05:00
|
|
|
def logger
|
|
|
|
ActionView::Base.logger || NullLogger
|
|
|
|
end
|
|
|
|
|
2016-02-16 18:21:16 -05:00
|
|
|
# Create a dependency tree for template named +name+.
|
|
|
|
def tree(name, finder, injected = [], partial = false, seen = {})
|
|
|
|
logical_name = name.gsub(%r|/_|, "/")
|
|
|
|
partial = partial || name.include?("/_")
|
2016-02-12 20:21:22 -05:00
|
|
|
|
2016-02-16 18:21:16 -05:00
|
|
|
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-16 18:26:16 -05:00
|
|
|
if node = seen[template.identifier] # handle cycles in the tree
|
|
|
|
node
|
2016-02-16 18:21:16 -05:00
|
|
|
else
|
|
|
|
node = seen[template.identifier] = Node.create(name, logical_name, template, partial)
|
|
|
|
|
|
|
|
deps = DependencyTracker.find_dependencies(name, template, finder.view_paths)
|
|
|
|
deps.uniq { |n| n.gsub(%r|/_|, "/") }.each do |dep_file|
|
|
|
|
node.children << tree(dep_file, finder, [], true, seen)
|
|
|
|
end
|
|
|
|
injected.each do |injected_dep|
|
|
|
|
node.children << Injected.new(injected_dep, nil, nil)
|
|
|
|
end
|
|
|
|
node
|
2016-02-15 17:25:10 -05:00
|
|
|
end
|
2016-02-16 18:21:16 -05:00
|
|
|
else
|
|
|
|
logger.error " '#{name}' file doesn't exist, so no dependencies"
|
|
|
|
logger.error " Couldn't find template for digesting: #{name}"
|
|
|
|
seen[name] ||= Missing.new(name, logical_name, nil)
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-16 17:25:34 -05:00
|
|
|
class Node
|
|
|
|
attr_reader :name, :logical_name, :template, :children
|
2016-02-15 17:32:41 -05:00
|
|
|
|
|
|
|
def self.create(name, logical_name, template, partial)
|
2016-02-16 18:21:16 -05:00
|
|
|
klass = partial ? Partial : Node
|
|
|
|
klass.new(name, logical_name, template, [])
|
2016-02-15 17:32:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(name, logical_name, template, children = [])
|
2016-02-16 18:21:16 -05:00
|
|
|
@name = name
|
2016-02-16 17:25:34 -05:00
|
|
|
@logical_name = logical_name
|
2016-02-16 18:21:16 -05:00
|
|
|
@template = template
|
|
|
|
@children = children
|
2016-02-15 17:32:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def digest(stack = [])
|
2016-02-15 17:25:10 -05:00
|
|
|
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
|
2016-02-16 18:10:25 -05:00
|
|
|
|
|
|
|
def to_dep_map
|
|
|
|
children.any? ? { name => children.map(&:to_dep_map) } : name
|
|
|
|
end
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 18:21:16 -05:00
|
|
|
class Partial < Node; end
|
2016-02-15 17:25:10 -05:00
|
|
|
|
2016-02-12 20:04:56 -05:00
|
|
|
class Missing < Node
|
2016-02-16 18:26:16 -05:00
|
|
|
def digest(_ = []) '' end
|
2016-02-12 20:04:56 -05:00
|
|
|
end
|
|
|
|
|
2016-02-15 17:25:10 -05:00
|
|
|
class Injected < Node
|
2016-02-16 18:26:16 -05:00
|
|
|
def digest(_ = []) name end
|
2016-02-15 17:25:10 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 18:21:16 -05:00
|
|
|
class NullLogger
|
|
|
|
def self.debug(_); end
|
|
|
|
def self.error(_); end
|
2012-10-17 18:20:51 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-05 10:05:50 -04:00
|
|
|
end
|