Documentation
¶
Index ¶
- Constants
- func PublishInvalidation(ctx context.Context, rdb *redis.Client, key string) error
- func PublishInvalidationMulti(ctx context.Context, rdb *redis.Client, keys ...string) error
- type Cache
- type HybridCache
- func (c *HybridCache) BLPop(ctx context.Context, timeout time.Duration, keys ...string) ([]string, error)
- func (c *HybridCache) Close() error
- func (c *HybridCache) Del(ctx context.Context, key string) error
- func (c *HybridCache) Eval(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error)
- func (c *HybridCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *HybridCache) Get(ctx context.Context, key string) (string, error)
- func (c *HybridCache) GetOrSet(ctx context.Context, key string, expiration time.Duration, ...) (string, error)
- func (c *HybridCache) GetRedisClient() *redis.Client
- func (c *HybridCache) Incr(ctx context.Context, key string) (int64, error)
- func (c *HybridCache) LTrim(ctx context.Context, key string, start, stop int64) error
- func (c *HybridCache) RPush(ctx context.Context, key string, values ...interface{}) error
- func (c *HybridCache) SAdd(ctx context.Context, key string, members ...interface{}) error
- func (c *HybridCache) SIsMember(ctx context.Context, key string, member interface{}) (bool, error)
- func (c *HybridCache) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
- func (c *HybridCache) StartL1Invalidator(rdb *redis.Client) error
- func (c *HybridCache) Stats(ctx context.Context) map[string]interface{}
- func (c *HybridCache) StopL1Invalidator() error
- type L1Invalidator
- type RedisCache
- func (c *RedisCache) BLPop(ctx context.Context, timeout time.Duration, keys ...string) ([]string, error)
- func (c *RedisCache) Client() *redis.Client
- func (c *RedisCache) Close() error
- func (c *RedisCache) Del(ctx context.Context, key string) error
- func (c *RedisCache) Eval(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error)
- func (c *RedisCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *RedisCache) Get(ctx context.Context, key string) (string, error)
- func (c *RedisCache) GetOrSet(ctx context.Context, key string, expiration time.Duration, ...) (string, error)
- func (c *RedisCache) Incr(ctx context.Context, key string) (int64, error)
- func (c *RedisCache) LTrim(ctx context.Context, key string, start, stop int64) error
- func (c *RedisCache) RPush(ctx context.Context, key string, values ...interface{}) error
- func (c *RedisCache) SAdd(ctx context.Context, key string, members ...interface{}) error
- func (c *RedisCache) SIsMember(ctx context.Context, key string, member interface{}) (bool, error)
- func (c *RedisCache) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
- func (c *RedisCache) Stats(ctx context.Context) map[string]interface{}
- type RistrettoCache
- func (c *RistrettoCache) BLPop(ctx context.Context, timeout time.Duration, keys ...string) ([]string, error)
- func (c *RistrettoCache) Close() error
- func (c *RistrettoCache) Del(ctx context.Context, key string) error
- func (c *RistrettoCache) Eval(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error)
- func (c *RistrettoCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *RistrettoCache) Get(ctx context.Context, key string) (string, error)
- func (c *RistrettoCache) GetOrSet(ctx context.Context, key string, expiration time.Duration, ...) (string, error)
- func (c *RistrettoCache) Incr(ctx context.Context, key string) (int64, error)
- func (c *RistrettoCache) LTrim(ctx context.Context, key string, start, stop int64) error
- func (c *RistrettoCache) RPush(ctx context.Context, key string, values ...interface{}) error
- func (c *RistrettoCache) SAdd(ctx context.Context, key string, members ...interface{}) error
- func (c *RistrettoCache) SIsMember(ctx context.Context, key string, member interface{}) (bool, error)
- func (c *RistrettoCache) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
- func (c *RistrettoCache) Stats(ctx context.Context) map[string]interface{}
Constants ¶
const (
// L1InvalidateChannel is the Redis Pub/Sub channel for L1 cache invalidation
L1InvalidateChannel = "cache:l1:invalidate"
)
Variables ¶
This section is empty.
Functions ¶
func PublishInvalidation ¶ added in v0.0.3
PublishInvalidation broadcasts a key invalidation to all pods via Redis Pub/Sub. This should be called whenever a cached value is updated or deleted.
Types ¶
type Cache ¶
type Cache interface {
Get(ctx context.Context, key string) (string, error)
Exists(ctx context.Context, key string) (bool, error)
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
Del(ctx context.Context, key string) error
Incr(ctx context.Context, key string) (int64, error)
RPush(ctx context.Context, key string, values ...interface{}) error
BLPop(ctx context.Context, timeout time.Duration, keys ...string) ([]string, error)
LTrim(ctx context.Context, key string, start, stop int64) error
Eval(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error)
SAdd(ctx context.Context, key string, members ...interface{}) error
SIsMember(ctx context.Context, key string, member interface{}) (bool, error)
// GetOrSet retrieves the value from cache or executes the fetch function, catching stampedes
GetOrSet(ctx context.Context, key string, expiration time.Duration, fetch func() (string, error)) (string, error)
// Stats returns cache statistics
Stats(ctx context.Context) map[string]interface{}
Close() error
}
Cache interface defines the methods for caching
type HybridCache ¶
type HybridCache struct {
// contains filtered or unexported fields
}
HybridCache combines local (L1) and remote (L2) caching
func NewHybridCache ¶
func NewHybridCache(local Cache, remote Cache) *HybridCache
NewHybridCache creates a new HybridCache
func (*HybridCache) Close ¶
func (c *HybridCache) Close() error
func (*HybridCache) GetRedisClient ¶
func (c *HybridCache) GetRedisClient() *redis.Client
GetRedisClient attempts to return the underlying Redis client if available
func (*HybridCache) RPush ¶
func (c *HybridCache) RPush(ctx context.Context, key string, values ...interface{}) error
func (*HybridCache) SAdd ¶
func (c *HybridCache) SAdd(ctx context.Context, key string, members ...interface{}) error
func (*HybridCache) StartL1Invalidator ¶ added in v0.0.3
func (c *HybridCache) StartL1Invalidator(rdb *redis.Client) error
StartL1Invalidator starts the L1 cache invalidator that subscribes to Redis Pub/Sub for cross-pod cache synchronization. This should be called after creating HybridCache.
func (*HybridCache) StopL1Invalidator ¶ added in v0.0.3
func (c *HybridCache) StopL1Invalidator() error
StopL1Invalidator stops the L1 cache invalidator
type L1Invalidator ¶ added in v0.0.3
type L1Invalidator struct {
// contains filtered or unexported fields
}
L1Invalidator subscribes to Redis Pub/Sub and clears local L1 cache when invalidation messages are received. This ensures L1 cache consistency across multiple pods/instances sharing the same Redis.
func NewL1Invalidator ¶ added in v0.0.3
func NewL1Invalidator(l1 Cache, rdb *redis.Client) *L1Invalidator
NewL1Invalidator creates a new L1Invalidator
func (*L1Invalidator) Start ¶ added in v0.0.3
func (i *L1Invalidator) Start() error
Start begins listening for invalidation messages from Redis Pub/Sub. This should be called once during application startup.
func (*L1Invalidator) Stop ¶ added in v0.0.3
func (i *L1Invalidator) Stop() error
Stop gracefully shuts down the invalidator
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache implements Cache using Redis
func NewRedisCache ¶
func NewRedisCache(client *redis.Client) *RedisCache
NewRedisCache creates a new RedisCache
func (*RedisCache) Client ¶
func (c *RedisCache) Client() *redis.Client
Client returns the underlying redis client
func (*RedisCache) Close ¶
func (c *RedisCache) Close() error
func (*RedisCache) RPush ¶
func (c *RedisCache) RPush(ctx context.Context, key string, values ...interface{}) error
func (*RedisCache) SAdd ¶
func (c *RedisCache) SAdd(ctx context.Context, key string, members ...interface{}) error
type RistrettoCache ¶
type RistrettoCache struct {
// contains filtered or unexported fields
}
RistrettoCache implements cache.Cache using In-Process Memory (Ristretto)
func NewRistrettoCache ¶
func NewRistrettoCache(maxKeys int64) (*RistrettoCache, error)
NewRistrettoCache creates a new RistrettoCache
func (*RistrettoCache) Close ¶
func (c *RistrettoCache) Close() error
func (*RistrettoCache) GetOrSet ¶
func (c *RistrettoCache) GetOrSet(ctx context.Context, key string, expiration time.Duration, fetch func() (string, error)) (string, error)
GetOrSet for L1
func (*RistrettoCache) Incr ¶
No-op for Redis specific methods (List, Set, Eval, etc.) If these are called on L1, they will error or panic. Ideally usage of L1 is mostly KV.
func (*RistrettoCache) RPush ¶
func (c *RistrettoCache) RPush(ctx context.Context, key string, values ...interface{}) error
func (*RistrettoCache) SAdd ¶
func (c *RistrettoCache) SAdd(ctx context.Context, key string, members ...interface{}) error