Repodb.Stmt_cache
repodb · API reference
Small statement cache used by concrete drivers.
The cache maps SQL strings to prepared statement handles and evicts the least-recently-used entry when max_size is reached. Drivers provide a finalize callback so evicted or cleared statements release backend resources correctly.
let cache =
Stmt_cache.create ~max_size:100 ~finalize:(fun stmt -> finalize stmt)
let stmt =
Stmt_cache.get_or_prepare cache ~sql
~prepare:(fun () -> prepare_statement sql)This module is intended for driver authors rather than application code. It does not parse SQL, normalize whitespace, or understand connection lifetime; use one cache per connection and call clear before closing that connection.
type 'stmt entry = {
stmt : 'stmt;
last_used : int;
}module StringHash : sig ... endtype 'stmt t = {
cache : (string, 'stmt entry) Kcas_data.Hashtbl.t;
max_size : int;
counter : int Kcas.Loc.t;
finalize : 'stmt -> unit;
}val create : max_size:int -> finalize:('a -> unit) -> 'a tval next_counter : 'a t -> intval evict_oldest : 'a t -> unitval get_or_prepare : 'a t -> sql:string -> prepare:(unit -> 'a) -> 'aval remove : 'a t -> sql:string -> unitval clear : 'a t -> unitval size : 'a t -> int