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 ... end
type '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 t
val next_counter : 'a t -> int
val evict_oldest : 'a t -> unit
val get_or_prepare : 'a t -> sql:string -> prepare:(unit -> 'a) -> 'a
val remove : 'a t -> sql:string -> unit
val clear : 'a t -> unit
val size : 'a t -> int