2012-12-13 08:47:33 -05:00
|
|
|
require 'thread_safe'
|
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)
|
2013-07-30 09:17:27 -04:00
|
|
|
@@cache = ThreadSafe::Cache.new
|
|
|
|
@@digest_monitor = Monitor.new
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def digest(name, format, finder, options = {})
|
|
|
|
cache_key = ([name, format] + Array.wrap(options[:dependencies])).join('.')
|
|
|
|
# this is a correctly done double-checked locking idiom
|
|
|
|
# (ThreadSafe::Cache's lookups have volatile semantics)
|
|
|
|
@@cache[cache_key] || @@digest_monitor.synchronize do
|
|
|
|
@@cache.fetch(cache_key) do # re-check under lock
|
|
|
|
compute_and_store_digest(cache_key, name, format, finder, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-07-07 12:44:16 -04:00
|
|
|
|
2013-07-30 09:17:27 -04:00
|
|
|
private
|
|
|
|
def compute_and_store_digest(cache_key, name, format, finder, options) # called under @@digest_monitor lock
|
|
|
|
klass = if options[:partial] || name.include?("/_")
|
|
|
|
# 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
|
|
|
|
|
|
|
|
@@cache[cache_key] = digest = klass.new(name, format, finder, options).digest # Store the actual digest
|
|
|
|
ensure
|
|
|
|
@@cache.delete_pair(cache_key, false) if pre_stored && !digest # something went wrong, make sure not to corrupt the @@cache
|
2012-10-17 18:20:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-08 13:20:47 -05:00
|
|
|
attr_reader :name, :format, :finder, :options
|
2012-10-17 18:20:51 -04:00
|
|
|
|
2013-01-08 13:20:47 -05:00
|
|
|
def initialize(name, format, finder, options={})
|
|
|
|
@name, @format, @finder, @options = name, format, finder, 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|
|
2012-08-29 15:23:15 -04:00
|
|
|
logger.try :info, "Cache digest for #{name}.#{format}: #{digest}"
|
|
|
|
end
|
|
|
|
rescue ActionView::MissingTemplate
|
|
|
|
logger.try :error, "Couldn't find template for digesting: #{name}.#{format}"
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
def dependencies
|
2012-12-16 17:17:53 -05:00
|
|
|
DependencyTracker.find_dependencies(name, template)
|
2012-08-29 15:23:15 -04:00
|
|
|
rescue ActionView::MissingTemplate
|
|
|
|
[] # File doesn't exist, so no dependencies
|
|
|
|
end
|
|
|
|
|
|
|
|
def nested_dependencies
|
|
|
|
dependencies.collect do |dependency|
|
2012-10-17 18:20:51 -04:00
|
|
|
dependencies = PartialDigestor.new(dependency, format, finder).nested_dependencies
|
2012-08-29 15:23:15 -04:00
|
|
|
dependencies.any? ? { dependency => dependencies } : dependency
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-09-05 12:51:24 -04:00
|
|
|
|
|
|
|
def logger
|
|
|
|
ActionView::Base.logger
|
|
|
|
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
|
2013-04-25 10:14:22 -04:00
|
|
|
@template ||= finder.find(logical_name, [], partial?, formats: [ format ])
|
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|
|
2012-12-13 08:47:33 -05:00
|
|
|
Digestor.digest(template_name, format, 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
|