- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (38)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (13)
- 5.1.7 (0)
- 5.2.3 (-1)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
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(Book.connection) do |params| Book.where(name: "my book").where("author_id > 3") end
The cached statement is executed by using the {connection.execute}[rdoc-ref:ConnectionAdapters::DatabaseStatements#execute] method:
cache.execute([], Book.connection)
The relation returned by the block is cached, and for each {execute}[rdoc-ref:ConnectionAdapters::DatabaseStatements#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(Book.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"], Book.connection)