aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/statement_cache.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-04-10 16:02:26 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-04-10 16:02:26 -0300
commit0235cdf5ce0e92b88d49b8e135e696a89b76d097 (patch)
tree2a5fe1d457ca2ea10e95448eaf84ca6605983741 /activerecord/lib/active_record/statement_cache.rb
parent0b38c84332a6e19250e4c2e34ebe46cd618360b6 (diff)
parentaf1a4bdc564864e80b234755651303dac68ae82e (diff)
downloadrails-0235cdf5ce0e92b88d49b8e135e696a89b76d097.tar.gz
rails-0235cdf5ce0e92b88d49b8e135e696a89b76d097.tar.bz2
rails-0235cdf5ce0e92b88d49b8e135e696a89b76d097.zip
Merge pull request #10152 from Noemj/statement_cache
Statement cache Conflicts: activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord/lib/active_record/statement_cache.rb')
-rw-r--r--activerecord/lib/active_record/statement_cache.rb26
1 files changed, 26 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/statement_cache.rb b/activerecord/lib/active_record/statement_cache.rb
new file mode 100644
index 0000000000..4a8c54414d
--- /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