Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 27 lines (24 sloc) 0.801 kB
0235cdf @rafaelfranca Merge pull request #10152 from Noemj/statement_cache
rafaelfranca authored
1 module ActiveRecord
af1a4bd @olliwer Added statement cache
olliwer authored
2
3 # Statement cache is used to cache a single statement in order to avoid creating the AST again.
4 # Initializing the cache is done by passing the statement in the initialization block:
5 #
062d1f7 Fix StatementCache docs format [ci skip]
Francesco Rodriguez authored
6 # cache = ActiveRecord::StatementCache.new do
7 # Book.where(name: "my book").limit(100)
8 # end
0235cdf @rafaelfranca Merge pull request #10152 from Noemj/statement_cache
rafaelfranca authored
9 #
af1a4bd @olliwer Added statement cache
olliwer authored
10 # The cached statement is executed by using the +execute+ method:
0235cdf @rafaelfranca Merge pull request #10152 from Noemj/statement_cache
rafaelfranca authored
11 #
062d1f7 Fix StatementCache docs format [ci skip]
Francesco Rodriguez authored
12 # cache.execute
af1a4bd @olliwer Added statement cache
olliwer authored
13 #
87735d3 minor edit on StatementCache documentation [ci skip]
Francesco Rodriguez authored
14 # The relation returned by the block is cached, and for each +execute+ call the cached relation gets duped.
af1a4bd @olliwer Added statement cache
olliwer authored
15 # Database is queried when +to_a+ is called on the relation.
0235cdf @rafaelfranca Merge pull request #10152 from Noemj/statement_cache
rafaelfranca authored
16 class StatementCache
af1a4bd @olliwer Added statement cache
olliwer authored
17 def initialize
18 @relation = yield
19 raise ArgumentError.new("Statement cannot be nil") if @relation.nil?
20 end
0235cdf @rafaelfranca Merge pull request #10152 from Noemj/statement_cache
rafaelfranca authored
21
af1a4bd @olliwer Added statement cache
olliwer authored
22 def execute
23 @relation.dup.to_a
24 end
25 end
26 end
Something went wrong with that request. Please try again.