Documentation
¶
Index ¶
- Variables
- type BatchCacher
- type BatchComputeFunc
- type BatchLocalCacherdeprecated
- type BatchRemoteCacherdeprecated
- type BatchTieredCache
- type Cacher
- type Coder
- type ComputeFunc
- type JSONCoder
- type LocalCacherdeprecated
- type MessagePackCoder
- type RedisCache
- func (r *RedisCache[V]) BatchGet(ctx context.Context, keys []string) (map[string]V, error)
- func (r *RedisCache[V]) BatchSet(ctx context.Context, items map[string]V, ttl time.Duration) error
- func (r *RedisCache[V]) Close() error
- func (r *RedisCache[V]) Delete(ctx context.Context, key string) error
- func (r *RedisCache[V]) Get(ctx context.Context, key string) (V, error)
- func (r *RedisCache[V]) Ping(ctx context.Context) error
- func (r *RedisCache[V]) Set(ctx context.Context, key string, value V, ttl time.Duration) error
- type RedisCacheConfig
- type RemoteCacherdeprecated
- type RistrettoCache
- func (r *RistrettoCache[V]) BatchGet(ctx context.Context, keys []string) (map[string]V, error)
- func (r *RistrettoCache[V]) BatchSet(ctx context.Context, items map[string]V, ttl time.Duration) error
- func (r *RistrettoCache[V]) Clear()
- func (r *RistrettoCache[V]) Close() error
- func (r *RistrettoCache[V]) Delete(ctx context.Context, key string) error
- func (r *RistrettoCache[V]) Get(ctx context.Context, key string) (V, error)
- func (r *RistrettoCache[V]) Metrics() *ristretto.Metrics
- func (r *RistrettoCache[V]) Set(ctx context.Context, key string, value V, ttl time.Duration) error
- type RistrettoCacheConfig
- type TieredCache
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCacheMiss indicates the key was not found in cache ErrCacheMiss = errors.New("cache miss") )
Functions ¶
This section is empty.
Types ¶
type BatchCacher ¶
type BatchCacher[V any] interface { Cacher[V] // BatchGet retrieves multiple values from cache // Returns a map of key-value pairs for found keys // Missing keys are simply not included in the returned map BatchGet(ctx context.Context, keys []string) (map[string]V, error) // BatchSet stores multiple values in cache with a TTL // All items share the same TTL BatchSet(ctx context.Context, items map[string]V, ttl time.Duration) error }
BatchCacher defines the interface for cache implementations that support batch operations
type BatchComputeFunc ¶
BatchComputeFunc is a function that computes multiple values when cache misses occur It receives a slice of keys and returns a map of key-value pairs
type BatchLocalCacher
deprecated
type BatchLocalCacher[V any] interface { BatchCacher[V] }
Deprecated: Use BatchCacher instead BatchLocalCacher defines the interface for local cache implementations that support batch operations
type BatchRemoteCacher
deprecated
type BatchRemoteCacher[V any] interface { BatchCacher[V] }
Deprecated: Use BatchCacher instead BatchRemoteCacher defines the interface for remote cache implementations that support batch operations
type BatchTieredCache ¶
type BatchTieredCache[V any] struct { // contains filtered or unexported fields }
BatchTieredCache implements multi-key cache operations with tiered caching strategy Strategy: caches[0] (L1) → caches[1] (L2) → ... → caches[n] (Ln) Optimized for batch operations where the compute function can fetch multiple keys efficiently
func NewBatchTieredCache ¶
func NewBatchTieredCache[V any](caches ...BatchCacher[V]) *BatchTieredCache[V]
NewBatchTieredCache creates a new batch tiered cache with dependency injection caches is a slice where caches[0] is L1 (fastest), caches[1] is L2, etc. Empty or nil caches in the slice are skipped
func (*BatchTieredCache[V]) BatchGet ¶
func (bc *BatchTieredCache[V]) BatchGet(ctx context.Context, keys []string, ttl time.Duration, batchComputeFn BatchComputeFunc[V]) (map[string]V, error)
BatchGet retrieves multiple values using the tiered caching strategy: 1. Check L1, L2, ..., Ln in order using BatchGet 2. For each tier hit, populate upper tiers 3. For all misses, execute batchComputeFn to fetch all at once 4. Populate all tiers with computed values Returns a map of successfully retrieved values (key -> value)
type Cacher ¶
type Cacher[V any] interface { // Get retrieves a value from cache // Returns ErrCacheMiss if the key is not found Get(ctx context.Context, key string) (V, error) // Set stores a value in cache with a TTL Set(ctx context.Context, key string, value V, ttl time.Duration) error // Delete removes a value from cache // Returns ErrCacheMiss if the key is not found Delete(ctx context.Context, key string) error }
Cacher defines the unified interface for cache implementations (local or remote) This interface can be used for multi-tier caching where caches[0] is L1, caches[1] is L2, etc.
type Coder ¶
type Coder[V any] interface { // Encode serializes a value to bytes Encode(value V) ([]byte, error) // Decode deserializes bytes to a value Decode(data []byte) (V, error) }
Coder defines the interface for encoding and decoding values
type ComputeFunc ¶
ComputeFunc is a function that computes the value when cache misses occur
type JSONCoder ¶
type JSONCoder[V any] struct{}
JSONCoder implements Coder using JSON encoding
func NewJSONCoder ¶
NewJSONCoder creates a new JSONCoder instance
type LocalCacher
deprecated
type MessagePackCoder ¶
type MessagePackCoder[V any] struct { // contains filtered or unexported fields }
MessagePackCoder implements Coder using MessagePack encoding
func NewMessagePackCoder ¶
func NewMessagePackCoder[V any]() *MessagePackCoder[V]
NewMessagePackCoder creates a new MessagePackCoder instance
func (*MessagePackCoder[V]) Decode ¶
func (c *MessagePackCoder[V]) Decode(data []byte) (V, error)
Decode deserializes MessagePack bytes to a value
func (*MessagePackCoder[V]) Encode ¶
func (c *MessagePackCoder[V]) Encode(value V) ([]byte, error)
Encode serializes a value to MessagePack bytes
type RedisCache ¶
type RedisCache[V any] struct { // contains filtered or unexported fields }
RedisCache wraps go-redis client to implement the RemoteCacher interface with generic type support
func NewRedisCache ¶
func NewRedisCache[V any](config *RedisCacheConfig, coder Coder[V]) (*RedisCache[V], error)
NewRedisCache creates a new RedisCache instance
func (*RedisCache[V]) BatchGet ¶
BatchGet retrieves multiple values from Redis using Pipeline Returns a map of key-value pairs for found keys Missing keys are simply not included in the returned map
func (*RedisCache[V]) BatchSet ¶
BatchSet stores multiple values in Redis with a TTL using Pipeline All items share the same TTL
func (*RedisCache[V]) Close ¶
func (r *RedisCache[V]) Close() error
Close closes the Redis connection
func (*RedisCache[V]) Delete ¶
func (r *RedisCache[V]) Delete(ctx context.Context, key string) error
Delete removes a value from Redis
func (*RedisCache[V]) Get ¶
func (r *RedisCache[V]) Get(ctx context.Context, key string) (V, error)
Get retrieves a value from Redis
type RedisCacheConfig ¶
type RedisCacheConfig struct {
// Addr is the Redis server address (e.g., "localhost:6379")
Addr string
// Password for Redis authentication (optional)
Password string
// DB is the Redis database number (0-15, default is 0)
DB int
// DialTimeout is the timeout for establishing new connections
DialTimeout time.Duration
// ReadTimeout is the timeout for socket reads
ReadTimeout time.Duration
// WriteTimeout is the timeout for socket writes
WriteTimeout time.Duration
// PoolSize is the maximum number of socket connections
PoolSize int
// MinIdleConns is the minimum number of idle connections
MinIdleConns int
}
RedisCacheConfig holds configuration for RedisCache
func DefaultRedisCacheConfig ¶
func DefaultRedisCacheConfig() *RedisCacheConfig
DefaultRedisCacheConfig returns a default configuration
type RemoteCacher
deprecated
type RistrettoCache ¶
type RistrettoCache[V any] struct { // contains filtered or unexported fields }
RistrettoCache wraps ristretto cache to implement the LocalCacher interface with generic type support
func NewRistrettoCache ¶
func NewRistrettoCache[V any](config *RistrettoCacheConfig) (*RistrettoCache[V], error)
NewRistrettoCache creates a new RistrettoCache instance
func (*RistrettoCache[V]) BatchGet ¶
BatchGet retrieves multiple values from the cache Returns a map of key-value pairs for found keys Missing keys are simply not included in the returned map
func (*RistrettoCache[V]) BatchSet ¶
func (r *RistrettoCache[V]) BatchSet(ctx context.Context, items map[string]V, ttl time.Duration) error
BatchSet stores multiple values in the cache with a TTL All items share the same TTL
func (*RistrettoCache[V]) Clear ¶
func (r *RistrettoCache[V]) Clear()
Clear removes all items from the cache
func (*RistrettoCache[V]) Close ¶
func (r *RistrettoCache[V]) Close() error
Close closes the cache and releases resources
func (*RistrettoCache[V]) Delete ¶
func (r *RistrettoCache[V]) Delete(ctx context.Context, key string) error
Delete removes a value from the cache
func (*RistrettoCache[V]) Get ¶
func (r *RistrettoCache[V]) Get(ctx context.Context, key string) (V, error)
Get retrieves a value from the cache
func (*RistrettoCache[V]) Metrics ¶
func (r *RistrettoCache[V]) Metrics() *ristretto.Metrics
Metrics returns cache metrics from ristretto
type RistrettoCacheConfig ¶
type RistrettoCacheConfig struct {
// NumCounters determines the number of keys tracked for admission & eviction.
// A good starting point is 10x the number of items you expect to keep in cache.
NumCounters int64
// MaxCost is the maximum total cost of items in cache.
// When cost is set to 1 per item, this effectively limits the number of items.
MaxCost int64
// BufferItems is the size of the Get/Set buffers.
// A larger buffer improves throughput but uses more memory.
BufferItems int64
}
func DefaultRistrettoCacheConfig ¶
func DefaultRistrettoCacheConfig() *RistrettoCacheConfig
type TieredCache ¶
type TieredCache[V any] struct { // contains filtered or unexported fields }
TieredCache implements a multi-tier caching strategy Strategy: caches[0] (L1) → caches[1] (L2) → ... → caches[n] (Ln) Uses singleflight to prevent cache stampede on compute function execution
func NewTieredCache ¶
func NewTieredCache[V any](caches ...Cacher[V]) *TieredCache[V]
NewTieredCache creates a new multi-tier cache with dependency injection caches is a slice where caches[0] is L1 (fastest), caches[1] is L2, etc. Empty or nil caches in the slice are skipped
func (*TieredCache[V]) Delete ¶
func (tc *TieredCache[V]) Delete(ctx context.Context, key string) error
Delete removes a key from all cache tiers
func (*TieredCache[V]) Get ¶
func (tc *TieredCache[V]) Get(ctx context.Context, key string, ttl time.Duration, computeFn ComputeFunc[V]) (V, error)
Get retrieves a value using the tiered caching strategy with compute function: 1. Check L1, L2, ..., Ln in order 2. If found in Li (i > 0), populate upper tiers (L0 to Li-1) 3. If not found in any tier, execute computeFn and populate all tiers Uses singleflight to ensure only one compute function executes per key concurrently