class PG::StatementCache

The prepared statements of a connection, by their SQL: the least recently used is closed when there are more than the capacity. Its names are qz1, qz2, ... Used by PG::Connection#query.

cache = PG::StatementCache.new(2)
cache.add("SELECT 1")   # => "qz1"
cache.add("SELECT 2")   # => "qz2"
cache.lookup("SELECT 1")   # => "qz1"
cache.add("SELECT 3")   # => "qz3"
cache.take_closes       # => ["qz2"]

src/statement_cache.qz:13

Classes and modules

Class methods

def self.new(capacity: Int) -> PG::StatementCache

A cache of at most capacity statements (0: none, every query is parsed afresh as the unnamed statement).

src/statement_cache.qz:28

def self.new(snapshot: PG::StatementCache::Snapshot) -> PG::StatementCache

A cache in the state snapshot recorded.

src/statement_cache.qz:31

Methods

def snapshot -> PG::StatementCache::Snapshot

The cache's state (the collections are immutable: nothing is copied).

src/statement_cache.qz:40

def enabled? -> Bool

Whether the cache keeps statements at all.

src/statement_cache.qz:43

def size -> Int

The number of statements kept.

src/statement_cache.qz:46

def lookup(sql: String) -> String?

The name of the statement prepared for sql, if any, which becomes the most recently used.

src/statement_cache.qz:50

def next_name -> String

A name for a new statement, not used yet.

src/statement_cache.qz:57

def add(sql: String, name: String? = …) -> String

Keeps sql prepared as name (a new name if none is given), and returns the name; the least recently used statement beyond the capacity is queued to be closed.

src/statement_cache.qz:65

def make_room -> Void

Makes room for one more statement: the least recently used beyond the capacity is queued to be closed, so that the request preparing the new one closes it first.

src/statement_cache.qz:78

def forget(sql: String) -> Void

Drops sql's statement, queuing it to be closed on the server.

src/statement_cache.qz:85

def drop(sql: String) -> Void

Drops sql's statement without closing it (the server has none).

src/statement_cache.qz:93

def clear -> Void

Drops every statement without closing them (DISCARD ALL, DEALLOCATE ALL: the server closed them).

src/statement_cache.qz:100

def take_closes -> Array[String]

The statements to close, which are then forgotten.

src/statement_cache.qz:107