aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods.rb
diff options
context:
space:
mode:
authorthedarkone <thedarkone2@gmail.com>2013-08-07 20:23:22 +0200
committerthedarkone <thedarkone2@gmail.com>2013-09-28 19:19:07 +0200
commit28572f59f828cc2b8309e8c477658fed2a38f83b (patch)
tree98f5e7dac1908b8ef13e0dd88c4ea9651fb97843 /activerecord/lib/active_record/attribute_methods.rb
parent2dea0dd099302de640aac28349569c002131c612 (diff)
downloadrails-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/lib/active_record/attribute_methods.rb')
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb20
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