Class: ActiveRecord::StatementCache
- Defined in:
- activerecord/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 create block:
cache = StatementCache.create(ClothingItem.lease_connection) do |params|
Book.where(name: "my book").where("author_id > 3")
end
The cached statement is executed by using the connection.execute method:
cache.execute([], ClothingItem.lease_connection)
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.
If you want to cache the statement without the values you can use the bind
method of the block parameter.
cache = StatementCache.create(ClothingItem.lease_connection) do |params|
Book.where(name: params.bind)
end
And pass the bind values as the first argument of execute
call.
cache.execute(["my book"], ClothingItem.lease_connection)
Defined Under Namespace
Classes: BindMap, Params, PartialQuery, PartialQueryCollector, Query, Substitute
Class Method Summary collapse
- .create(connection, callable = nil, &block) ⇒ Object
- .partial_query ⇒ Object
- .partial_query_collector ⇒ Object
- .query ⇒ Object
- .unsupported_value?(value) ⇒ Boolean
Instance Method Summary collapse
- #execute(params, connection, async: false, &block) ⇒ Object
-
#initialize(query_builder, bind_map, model) ⇒ StatementCache
constructor
A new instance of StatementCache.
Constructor Details
#initialize(query_builder, bind_map, model) ⇒ StatementCache
Returns a new instance of StatementCache.
143 144 145 146 147 |
# File 'activerecord/lib/active_record/statement_cache.rb', line 143 def initialize(query_builder, bind_map, model) @query_builder = query_builder @bind_map = bind_map @model = model end |
Class Method Details
.create(connection, callable = nil, &block) ⇒ Object
136 137 138 139 140 141 |
# File 'activerecord/lib/active_record/statement_cache.rb', line 136 def self.create(connection, callable = nil, &block) relation = (callable || block).call Params.new query_builder, binds = connection.cacheable_query(self, relation.arel) bind_map = BindMap.new(binds) new(query_builder, bind_map, relation.model) end |
.partial_query ⇒ Object
105 106 107 |
# File 'activerecord/lib/active_record/statement_cache.rb', line 105 def self.partial_query(...) PartialQuery.new(...) end |
.partial_query_collector ⇒ Object
109 110 111 |
# File 'activerecord/lib/active_record/statement_cache.rb', line 109 def self.partial_query_collector PartialQueryCollector.new end |
Instance Method Details
#execute(params, connection, async: false, &block) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'activerecord/lib/active_record/statement_cache.rb', line 149 def execute(params, connection, async: false, &block) bind_values = @bind_map.bind params sql = @query_builder.sql_for bind_values, connection if async @model.async_find_by_sql(sql, bind_values, preparable: true, allow_retry: @query_builder.retryable, &block) else @model.find_by_sql(sql, bind_values, preparable: true, allow_retry: @query_builder.retryable, &block) end rescue ::RangeError async ? Promise.wrap([]) : [] end |