cache

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cache provides caching utilities for database prepared statements.

Index

Constants

View Source
const (
	// DefaultStmtCacheCapacity is the default maximum number of cached prepared statements.
	DefaultStmtCacheCapacity = 1000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Stats

type Stats struct {
	Size      int     // Current number of cached statements.
	Capacity  int     // Maximum capacity.
	Hits      uint64  // Number of successful cache lookups.
	Misses    uint64  // Number of cache misses.
	Evictions uint64  // Number of evicted statements.
	HitRate   float64 // Cache hit rate (hits / total requests).
}

Stats holds cache performance metrics.

type StmtCache

type StmtCache struct {
	// contains filtered or unexported fields
}

StmtCache stores prepared statements with LRU eviction policy.

func NewStmtCache

func NewStmtCache() *StmtCache

NewStmtCache creates a new prepared statement cache with default capacity.

func NewStmtCacheWithCapacity

func NewStmtCacheWithCapacity(capacity int) *StmtCache

NewStmtCacheWithCapacity creates a new prepared statement cache with specified capacity.

func (*StmtCache) Clear

func (sc *StmtCache) Clear()

Clear closes and removes all cached prepared statements.

func (*StmtCache) Get

func (sc *StmtCache) Get(key string) (*sql.Stmt, bool)

Get retrieves a prepared statement from cache by SQL query string. Returns the statement and true if found, nil and false otherwise. Accessing a statement moves it to the front of the LRU list.

func (*StmtCache) IsPinned

func (sc *StmtCache) IsPinned(key string) bool

IsPinned returns true if the given key is pinned.

func (*StmtCache) Pin

func (sc *StmtCache) Pin(key string) bool

Pin marks a cached statement as pinned, preventing it from being evicted. Pinned statements remain in cache until explicitly unpinned or cleared. Returns true if the key was found and pinned, false otherwise.

func (*StmtCache) Set

func (sc *StmtCache) Set(key string, stmt *sql.Stmt)

Set stores a prepared statement in cache with SQL query string as key. If the cache is at capacity, the least recently used statement is evicted and closed.

func (*StmtCache) Stats

func (sc *StmtCache) Stats() Stats

Stats returns cache statistics.

func (*StmtCache) Unpin

func (sc *StmtCache) Unpin(key string) bool

Unpin removes the pin from a cached statement, allowing it to be evicted normally. Returns true if the key was found and unpinned, false otherwise.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL