diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-11-24 11:24:31 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-11-24 11:24:31 -0700 |
commit | 72e8442797b90d865811e39418ef9d49238b193a (patch) | |
tree | d1a36e5d934eb621a543fe6907022050a7d5c132 | |
parent | 81e0f010e88bc4eca1df0af51c5c0aae1bce4b48 (diff) | |
download | rails-72e8442797b90d865811e39418ef9d49238b193a.tar.gz rails-72e8442797b90d865811e39418ef9d49238b193a.tar.bz2 rails-72e8442797b90d865811e39418ef9d49238b193a.zip |
Ensure the type map's cache is thread safe
Thanks to @thedarkone for pointing out that an instance of this object
is used in a shared context.
-rw-r--r-- | activerecord/lib/active_record/type/type_map.rb | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/type/type_map.rb b/activerecord/lib/active_record/type/type_map.rb index 7c194c0cdf..09f5ba6b74 100644 --- a/activerecord/lib/active_record/type/type_map.rb +++ b/activerecord/lib/active_record/type/type_map.rb @@ -1,10 +1,12 @@ +require 'thread_safe' + module ActiveRecord module Type class TypeMap # :nodoc: def initialize @mapping = {} - @cache = Hash.new do |h, key| - h[key] = {} + @cache = ThreadSafe::Cache.new do |h, key| + h.fetch_or_store(key, ThreadSafe::Cache.new) end end @@ -13,7 +15,9 @@ module ActiveRecord end def fetch(lookup_key, *args, &block) - @cache[lookup_key][args] ||= perform_fetch(lookup_key, *args, &block) + @cache[lookup_key].fetch_or_store(args) do + perform_fetch(lookup_key, *args, &block) + end end def register_type(key, value = nil, &block) |