diff options
author | thedarkone <thedarkone2@gmail.com> | 2013-08-07 20:23:22 +0200 |
---|---|---|
committer | thedarkone <thedarkone2@gmail.com> | 2013-09-28 19:19:07 +0200 |
commit | 28572f59f828cc2b8309e8c477658fed2a38f83b (patch) | |
tree | 98f5e7dac1908b8ef13e0dd88c4ea9651fb97843 /activerecord | |
parent | 2dea0dd099302de640aac28349569c002131c612 (diff) | |
download | rails-28572f59f828cc2b8309e8c477658fed2a38f83b.tar.gz rails-28572f59f828cc2b8309e8c477658fed2a38f83b.tar.bz2 rails-28572f59f828cc2b8309e8c477658fed2a38f83b.zip |
Use TS::Cache instead of Mutex + Hash.
TS::Cache#compute_if_absent guarantees that only a single thread will get to execute the provided block for a given key.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 208da2cb77..bf270c1829 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -1,5 +1,6 @@ require 'active_support/core_ext/enumerable' require 'mutex_m' +require 'thread_safe' module ActiveRecord # = Active Record Attribute Methods @@ -29,23 +30,18 @@ module ActiveRecord } class AttributeMethodCache - include Mutex_m - def initialize - super @module = Module.new - @method_cache = {} + @method_cache = ThreadSafe::Cache.new end def [](name) - synchronize do - @method_cache.fetch(name) { - safe_name = name.unpack('h*').first - temp_method = "__temp__#{safe_name}" - ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name - @module.module_eval method_body(temp_method, safe_name), __FILE__, __LINE__ - @method_cache[name] = @module.instance_method temp_method - } + @method_cache.compute_if_absent(name) do + safe_name = name.unpack('h*').first + temp_method = "__temp__#{safe_name}" + ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name + @module.module_eval method_body(temp_method, safe_name), __FILE__, __LINE__ + @module.instance_method temp_method end end |