Documentation
¶
Overview ¶
Package cache provides caching utilities for database prepared statements.
Index ¶
- Constants
- type Stats
- type StmtCache
- func (sc *StmtCache) Clear()
- func (sc *StmtCache) Get(key string) (*sql.Stmt, bool)
- func (sc *StmtCache) IsPinned(key string) bool
- func (sc *StmtCache) Pin(key string) bool
- func (sc *StmtCache) Set(key string, stmt *sql.Stmt)
- func (sc *StmtCache) Stats() Stats
- func (sc *StmtCache) Unpin(key string) bool
Constants ¶
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 ¶
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 ¶
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) Pin ¶
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.