aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorNoemj <olli.rissanen@helsinki.fi>2013-04-10 17:40:01 +0300
committerOlli Rissanen <olli.rissanen@helsinki.fi>2013-04-10 21:20:58 +0300
commitaf1a4bdc564864e80b234755651303dac68ae82e (patch)
tree2345aada72f625bf1ce2a582625ae9fbef715bb6 /activerecord/lib
parent45321a69b3c01ce8a4cfef6cdf13381d73e10cd3 (diff)
downloadrails-af1a4bdc564864e80b234755651303dac68ae82e.tar.gz
rails-af1a4bdc564864e80b234755651303dac68ae82e.tar.bz2
rails-af1a4bdc564864e80b234755651303dac68ae82e.zip
Added statement cache
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record.rb1
-rw-r--r--activerecord/lib/active_record/statement_cache.rb26
2 files changed, 27 insertions, 0 deletions
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb
index c33f03f13f..58a362cb46 100644
--- a/activerecord/lib/active_record.rb
+++ b/activerecord/lib/active_record.rb
@@ -56,6 +56,7 @@ module ActiveRecord
autoload :SchemaMigration
autoload :Scoping
autoload :Serialization
+ autoload :StatementCache
autoload :Store
autoload :Timestamp
autoload :Transactions
diff --git a/activerecord/lib/active_record/statement_cache.rb b/activerecord/lib/active_record/statement_cache.rb
new file mode 100644
index 0000000000..1ef856a0e0
--- /dev/null
+++ b/activerecord/lib/active_record/statement_cache.rb
@@ -0,0 +1,26 @@
+module ActiveRecord
+
+ # Statement cache is used to cache a single statement in order to avoid creating the AST again.
+ # Initializing the cache is done by passing the statement in the initialization block:
+ #
+ # cache = ActiveRecord::StatementCache.new do
+ # Book.where(:name => "my book").limit(100)
+ # end
+ #
+ # The cached statement is executed by using the +execute+ method:
+ #
+ # cache.execute
+ #
+ # The relation returned by yield 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?
+ end
+
+ def execute
+ @relation.dup.to_a
+ end
+ end
+end