Class: ActiveRecord::StatementCache
- Inherits:
-
Object
- Object
- ActiveRecord::StatementCache
- Defined in:
- lib/active_record/statement_cache.rb
Overview
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 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.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize ⇒ StatementCache
constructor
A new instance of StatementCache.
Constructor Details
#initialize ⇒ StatementCache
Returns a new instance of StatementCache.
17 18 19 20 |
# File 'lib/active_record/statement_cache.rb', line 17 def initialize @relation = yield raise ArgumentError.new("Statement cannot be nil") if @relation.nil? end |
Instance Method Details
#execute ⇒ Object
22 23 24 |
# File 'lib/active_record/statement_cache.rb', line 22 def execute @relation.dup.to_a end |