1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

fix Digestor to be thread safe.

Add a lock on the cache so that we don't have concurrency issues when
calculating the cache.
This commit is contained in:
Aaron Patterson 2012-10-17 15:20:51 -07:00
parent d410ac5136
commit 01149dd4f9

View file

@ -1,3 +1,5 @@
require 'mutex_m'
module ActionView
class Digestor
EXPLICIT_DEPENDENCY = /# Template Dependency: ([^ ]+)/
@ -19,16 +21,30 @@ module ActionView
/x
cattr_reader(:cache)
@@cache = Hash.new
@@cache = Hash.new.extend Mutex_m
def self.digest(name, format, finder, options = {})
cache["#{name}.#{format}"] ||= new(name, format, finder, options).digest
cache.synchronize do
unsafe_digest name, format, finder, options
end
end
attr_reader :name, :format, :finder, :options
###
# This method is NOT thread safe. DO NOT CALL IT DIRECTLY, instead call
# Digestor.digest
def self.unsafe_digest(name, format, finder, options = {}) # :nodoc:
key = "#{name}.#{format}"
def initialize(name, format, finder, options = {})
@name, @format, @finder, @options = name, format, finder, options
cache.fetch(key) do
klass = options[:partial] || name.include?("/_") ? PartialDigestor : Digestor
cache[key] = klass.new(name, format, finder).digest
end
end
attr_reader :name, :format, :finder
def initialize(name, format, finder)
@name, @format, @finder = name, format, finder
end
def digest
@ -48,7 +64,7 @@ module ActionView
def nested_dependencies
dependencies.collect do |dependency|
dependencies = Digestor.new(dependency, format, finder, partial: true).nested_dependencies
dependencies = PartialDigestor.new(dependency, format, finder).nested_dependencies
dependencies.any? ? { dependency => dependencies } : dependency
end
end
@ -68,7 +84,7 @@ module ActionView
end
def partial?
options[:partial] || name.include?("/_")
false
end
def source
@ -77,7 +93,7 @@ module ActionView
def dependency_digest
dependencies.collect do |template_name|
Digestor.digest(template_name, format, finder, partial: true)
Digestor.unsafe_digest(template_name, format, finder, partial: true)
end.join("-")
end
@ -101,4 +117,10 @@ module ActionView
source.scan(EXPLICIT_DEPENDENCY).flatten.uniq
end
end
class PartialDigestor < Digestor # :nodoc:
def partial?
true
end
end
end