aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/statement_cache.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2014-01-14 14:26:00 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2014-01-14 14:26:00 -0800
commit74bfbfdb028bf4fb97fdf93451caeb49aa89ff39 (patch)
tree316ff960d8e4b746aef1451d011037a4b93c3b67 /activerecord/lib/active_record/statement_cache.rb
parenta924e0dbcd08f6cb72a5afb093f7cc9c1b867e2b (diff)
downloadrails-74bfbfdb028bf4fb97fdf93451caeb49aa89ff39.tar.gz
rails-74bfbfdb028bf4fb97fdf93451caeb49aa89ff39.tar.bz2
rails-74bfbfdb028bf4fb97fdf93451caeb49aa89ff39.zip
fix cache class interface
Diffstat (limited to 'activerecord/lib/active_record/statement_cache.rb')
-rw-r--r--activerecord/lib/active_record/statement_cache.rb22
1 files changed, 17 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/statement_cache.rb b/activerecord/lib/active_record/statement_cache.rb
index dd4ee0c4a0..90d4748d84 100644
--- a/activerecord/lib/active_record/statement_cache.rb
+++ b/activerecord/lib/active_record/statement_cache.rb
@@ -14,13 +14,25 @@ module ActiveRecord
# The relation returned by the block is cached, and for each +execute+ call the cached relation gets duped.
# Database is queried when +to_a+ is called on the relation.
class StatementCache
- def initialize
- @relation = yield
- raise ArgumentError.new("Statement cannot be nil") if @relation.nil?
+ def initialize(block = Proc.new)
+ @mutex = Mutex.new
+ @relation = nil
+ @block = block
end
- def execute
- @relation.dup.to_a
+ def execute(*vals)
+ rel = relation vals
+ @mutex.synchronize do
+ rel.set_binds vals
+ rel.to_a
+ end
+ end
+
+ private
+ def relation(values)
+ @relation || @mutex.synchronize {
+ @block.call(*values)
+ }
end
end
end