Documentation
¶
Overview ¶
Package cache provides a small, driver-based caching abstraction with pluggable in-memory and Redis backends, TTL helpers, and a cache-aside "Remember" helper for wrapping expensive or slow operations.
Index ¶
- Variables
- func NewRedisClient(ctx context.Context, options ...RedisClientOption) (*redis.Client, error)
- type Cache
- func (c *Cache[Value]) Delete(ctx context.Context, key Key) error
- func (c *Cache[Value]) Driver() Driver[Value]
- func (c *Cache[Value]) Get(ctx context.Context, key Key) (Value, bool, error)
- func (c *Cache[Value]) Has(ctx context.Context, key Key) (bool, error)
- func (c *Cache[Value]) Remember(ctx context.Context, key Key, ttl TTL, ...) (Value, error)
- func (c *Cache[Value]) Set(ctx context.Context, key Key, value Value, ttl TTL) error
- type Cacher
- type Driver
- type Hit
- type Key
- type MemoryConfig
- type MemoryDriver
- func (d *MemoryDriver[Value]) Delete(_ context.Context, key Key) error
- func (d *MemoryDriver[Value]) Get(_ context.Context, key Key) (Value, bool, error)
- func (d *MemoryDriver[Value]) Has(ctx context.Context, key Key) (bool, error)
- func (d *MemoryDriver[Value]) Set(_ context.Context, key Key, value Value, ttl TTL) error
- type MemoryOption
- type RedisClientConfig
- type RedisClientOption
- func WithRedisAddr(addr string) RedisClientOption
- func WithRedisDB(db int) RedisClientOption
- func WithRedisMaxRetries(retries int) RedisClientOption
- func WithRedisMinIdleConns(conns int) RedisClientOption
- func WithRedisPassword(password string) RedisClientOption
- func WithRedisPoolSize(size int) RedisClientOption
- func WithRedisTLS(enable bool) RedisClientOption
- func WithRedisTimeouts(dial, read, write, idle, maxConnAge time.Duration) RedisClientOption
- func WithRedisUsername(username string) RedisClientOption
- type RedisDriver
- func (d *RedisDriver[Value]) Client() *redis.Client
- func (d *RedisDriver[Value]) Close() error
- func (d *RedisDriver[Value]) Delete(ctx context.Context, key Key) error
- func (d *RedisDriver[Value]) Exists(ctx context.Context, keys ...Key) (int64, error)
- func (d *RedisDriver[Value]) FlushDB(ctx context.Context) error
- func (d *RedisDriver[Value]) Get(ctx context.Context, key Key) (Value, bool, error)
- func (d *RedisDriver[Value]) Has(ctx context.Context, key Key) (bool, error)
- func (d *RedisDriver[Value]) Keys(ctx context.Context, pattern string) ([]string, error)
- func (d *RedisDriver[Value]) Ping(ctx context.Context) error
- func (d *RedisDriver[Value]) Set(ctx context.Context, key Key, value Value, ttl TTL) error
- func (d *RedisDriver[Value]) TTL(ctx context.Context, key Key) (time.Duration, error)
- type RedisDriverConfig
- type RedisDriverOption
- type TTL
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultTTL tells the driver to use whatever default TTL it was configured with. DefaultTTL = TTL{/* contains filtered or unexported fields */} // Forever stores an entry without expiration. Forever = TTL{/* contains filtered or unexported fields */} )
var ErrCacheSetFailed = errors.New("cache set operation failed")
ErrCacheSetFailed is returned by Remember when the value was fetched successfully but could not be written back to the cache.
Functions ¶
func NewRedisClient ¶
NewRedisClient creates a new Redis client from the provided options
Types ¶
type Cache ¶
type Cache[Value any] struct { // contains filtered or unexported fields }
Cache is the main cache implementation that uses a driver
func NewMemoryCache ¶
func NewMemoryCache[Value any](options ...MemoryOption) *Cache[Value]
NewMemoryCache creates a new cache instance with a memory driver This is a convenience function that combines NewMemoryDriver and NewCache
func NewRedisCache ¶
func NewRedisCache[Value any](client *redis.Client, options ...RedisDriverOption) *Cache[Value]
NewRedisCache creates a new cache instance with a Redis driver This is a convenience function that combines NewRedisDriver and NewCache
type Cacher ¶
type Cacher[Value any] interface { Get(ctx context.Context, key Key) (value Value, found bool, err error) Set(ctx context.Context, key Key, value Value, ttl TTL) error Has(ctx context.Context, key Key) (bool, error) Delete(ctx context.Context, key Key) error Remember(ctx context.Context, key Key, ttl TTL, fetch func(ctx context.Context) (Value, error)) (Value, error) }
Cacher is the main cache interface
type Driver ¶
type Driver[Value any] interface { Get(ctx context.Context, key Key) (value Value, found bool, err error) Set(ctx context.Context, key Key, value Value, ttl TTL) error Has(ctx context.Context, key Key) (bool, error) Delete(ctx context.Context, key Key) error }
Driver is the interface that cache drivers must implement Drivers handle the low-level storage operations
type MemoryConfig ¶
MemoryConfig holds configuration for memory driver
type MemoryDriver ¶
type MemoryDriver[Value any] struct { // contains filtered or unexported fields }
MemoryDriver implements the Driver interface using in-memory storage
func NewMemoryDriver ¶
func NewMemoryDriver[Value any](options ...MemoryOption) *MemoryDriver[Value]
NewMemoryDriver creates a new memory driver instance
func (*MemoryDriver[Value]) Delete ¶
func (d *MemoryDriver[Value]) Delete(_ context.Context, key Key) error
Delete removes a key from memory storage
type MemoryOption ¶
type MemoryOption func(*MemoryConfig)
MemoryOption is a function that configures memory driver
func WithMemoryDefaultTTL ¶
func WithMemoryDefaultTTL(duration time.Duration) MemoryOption
WithMemoryDefaultTTL sets the default TTL for memory driver
type RedisClientConfig ¶
type RedisClientConfig struct {
Addr string // Redis server address (e.g., "localhost:6379")
Username string // Redis username (empty for no auth)
Password string // Redis password (empty for no auth)
DB int // Redis database number
UseTLS bool // Enable TLS for Redis connection
PoolSize int // Maximum number of socket connections
MinIdleConns int // Minimum number of idle connections
MaxRetries int // Maximum number of retries
DialTimeout time.Duration // Dial timeout for establishing new connections
ReadTimeout time.Duration // Timeout for socket reads
WriteTimeout time.Duration // Timeout for socket writes
IdleTimeout time.Duration // Close connections after remaining idle for this duration
MaxConnAge time.Duration // Close connections older than this duration
}
RedisClientConfig holds configuration for Redis client
type RedisClientOption ¶
type RedisClientOption func(*RedisClientConfig)
RedisClientOption is a function that configures Redis client
func WithRedisAddr ¶
func WithRedisAddr(addr string) RedisClientOption
WithRedisAddr sets the Redis server address
func WithRedisDB ¶
func WithRedisDB(db int) RedisClientOption
WithRedisDB sets the Redis database number
func WithRedisMaxRetries ¶
func WithRedisMaxRetries(retries int) RedisClientOption
WithRedisMaxRetries sets the maximum number of retries
func WithRedisMinIdleConns ¶
func WithRedisMinIdleConns(conns int) RedisClientOption
WithRedisMinIdleConns sets the minimum number of idle connections
func WithRedisPassword ¶
func WithRedisPassword(password string) RedisClientOption
WithRedisPassword sets the Redis password
func WithRedisPoolSize ¶
func WithRedisPoolSize(size int) RedisClientOption
WithRedisPoolSize sets the maximum number of socket connections
func WithRedisTLS ¶
func WithRedisTLS(enable bool) RedisClientOption
WithRedisTLS enables or disables TLS for Redis connection
func WithRedisTimeouts ¶
func WithRedisTimeouts(dial, read, write, idle, maxConnAge time.Duration) RedisClientOption
WithRedisTimeouts sets various timeout configurations
func WithRedisUsername ¶
func WithRedisUsername(username string) RedisClientOption
WithRedisUsername sets the Redis username
type RedisDriver ¶
type RedisDriver[Value any] struct { // contains filtered or unexported fields }
RedisDriver implements the Driver interface using Redis
func NewRedisDriver ¶
func NewRedisDriver[Value any](client *redis.Client, options ...RedisDriverOption) *RedisDriver[Value]
NewRedisDriver creates a new Redis driver instance with the provided client
func (*RedisDriver[Value]) Client ¶
func (d *RedisDriver[Value]) Client() *redis.Client
Client returns the underlying Redis client for advanced operations (pipelines, transactions, pub/sub, streams, custom commands).
Note that key prefixes configured via WithRedisPrefix option are not automatically applied when using the client directly.
func (*RedisDriver[Value]) Close ¶
func (d *RedisDriver[Value]) Close() error
Close closes the Redis connection
func (*RedisDriver[Value]) Delete ¶
func (d *RedisDriver[Value]) Delete(ctx context.Context, key Key) error
Delete removes a key from Redis
func (*RedisDriver[Value]) FlushDB ¶
func (d *RedisDriver[Value]) FlushDB(ctx context.Context) error
FlushDB flushes the current database (use with caution)
func (*RedisDriver[Value]) Ping ¶
func (d *RedisDriver[Value]) Ping(ctx context.Context) error
Ping tests the connection to Redis
type RedisDriverConfig ¶
type RedisDriverConfig struct {
DefaultTTL time.Duration // Default TTL for cache entries
Prefix string // Prefix for all keys
}
RedisDriverConfig holds configuration for Redis driver
type RedisDriverOption ¶
type RedisDriverOption func(*RedisDriverConfig)
RedisDriverOption is a function that configures Redis driver
func WithRedisDefaultTTL ¶
func WithRedisDefaultTTL(duration time.Duration) RedisDriverOption
WithRedisDefaultTTL sets the default TTL for Redis driver
func WithRedisPrefix ¶
func WithRedisPrefix(prefix string) RedisDriverOption
WithRedisPrefix sets the prefix for all keys in Redis driver
type TTL ¶
type TTL struct {
// contains filtered or unexported fields
}
TTL describes how long a cache entry should live. Build one with DefaultTTL, Forever, WithTTL, or WithTTLUntil.
func WithTTLUntil ¶
WithTTLUntil builds a TTL that expires at the given point in time.
It's handy when you already have an absolute expiry (e.g. an "expires_at" column from a database) and just need to convert it into a relative TTL.
Internally this is time.Until(t): if t is in the past the resulting duration is negative, and drivers that hand it straight to a backend such as Redis may expire the key immediately. Make sure t is in the future unless that's what you want.