cache

package
v0.0.0-...-dac86b4 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheConfig

type CacheConfig struct {
	// MaxSize is the maximum number of items in the cache.
	MaxSize int
	// DefaultTTL is the default time-to-live for cache entries.
	DefaultTTL time.Duration
}

CacheConfig configures the cache layer.

func DefaultCacheConfig

func DefaultCacheConfig() CacheConfig

DefaultCacheConfig returns sensible defaults.

type CacheLayer

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

CacheLayer implements a thread-safe cache with TTL expiration and LRU eviction. It follows the cache-aside pattern: callers check the cache first, then fall back to the source of truth and populate the cache on miss.

func NewCacheLayer

func NewCacheLayer(cfg CacheConfig) *CacheLayer

NewCacheLayer creates a new cache layer.

func (*CacheLayer) Clear

func (c *CacheLayer) Clear()

Clear removes all entries from the cache.

func (*CacheLayer) Delete

func (c *CacheLayer) Delete(key string)

Delete removes a key from the cache.

func (*CacheLayer) Get

func (c *CacheLayer) Get(key string) (any, bool)

Get retrieves a value from the cache. Returns the value and true if found and not expired, or nil and false on miss.

func (*CacheLayer) GetOrSet

func (c *CacheLayer) GetOrSet(key string, loader func() (any, error)) (any, error)

GetOrSet atomically gets a value or computes and stores it if missing. The loader function is called only on cache miss.

func (*CacheLayer) GetOrSetWithTTL

func (c *CacheLayer) GetOrSetWithTTL(key string, ttl time.Duration, loader func() (any, error)) (any, error)

GetOrSetWithTTL atomically gets a value or computes and stores it if missing, with a specific TTL.

func (*CacheLayer) Len

func (c *CacheLayer) Len() int

Len returns the number of items in the cache (including expired but not yet evicted).

func (*CacheLayer) PurgeExpired

func (c *CacheLayer) PurgeExpired() int

PurgeExpired removes all expired entries from the cache. This can be called periodically for proactive cleanup.

func (*CacheLayer) Set

func (c *CacheLayer) Set(key string, value any)

Set stores a value in the cache with the default TTL.

func (*CacheLayer) SetWithTTL

func (c *CacheLayer) SetWithTTL(key string, value any, ttl time.Duration)

SetWithTTL stores a value in the cache with a specific TTL.

func (*CacheLayer) Stats

func (c *CacheLayer) Stats() CacheStats

Stats returns cache statistics.

type CacheStats

type CacheStats struct {
	Size      int
	MaxSize   int
	Hits      int64
	Misses    int64
	Evictions int64
	HitRate   float64
}

CacheStats holds cache statistics.

type Conn

type Conn interface {
	// Close returns the connection to the pool or destroys it.
	Close() error
	// Ping checks if the connection is still alive.
	Ping(ctx context.Context) error
}

Conn represents a pooled connection.

type ConnFactory

type ConnFactory func(ctx context.Context) (Conn, error)

ConnFactory creates new connections.

type ConnectionPool

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

ConnectionPool manages a pool of reusable connections with health checking.

func NewConnectionPool

func NewConnectionPool(cfg ConnectionPoolConfig, factory ConnFactory) *ConnectionPool

NewConnectionPool creates a new connection pool.

func (*ConnectionPool) Acquire

func (p *ConnectionPool) Acquire(ctx context.Context) (Conn, error)

Acquire obtains a connection from the pool.

func (*ConnectionPool) Close

func (p *ConnectionPool) Close() error

Close shuts down the pool and closes all connections.

func (*ConnectionPool) Start

func (p *ConnectionPool) Start(ctx context.Context) error

Start initializes the pool with minimum connections and starts the health checker.

func (*ConnectionPool) Stats

Stats returns current pool statistics.

type ConnectionPoolConfig

type ConnectionPoolConfig struct {
	// MinConns is the minimum number of connections kept open.
	MinConns int
	// MaxConns is the maximum number of connections allowed.
	MaxConns int
	// IdleTimeout is how long an idle connection is kept before being closed.
	IdleTimeout time.Duration
	// HealthCheckInterval is how often idle connections are health-checked.
	HealthCheckInterval time.Duration
	// AcquireTimeout is the maximum time to wait for a connection.
	AcquireTimeout time.Duration
}

ConnectionPoolConfig configures the connection pool.

func DefaultConnectionPoolConfig

func DefaultConnectionPoolConfig() ConnectionPoolConfig

DefaultConnectionPoolConfig returns sensible defaults.

type ConnectionPoolStats

type ConnectionPoolStats struct {
	TotalConns   int
	IdleConns    int
	ActiveConns  int
	AcquireCount int64
	AcquireErr   int64
	MaxConns     int
}

ConnectionPoolStats holds pool statistics.

Jump to

Keyboard shortcuts

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