cache

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
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

func PublishInvalidation(ctx context.Context, rdb *redis.Client, key string) error

PublishInvalidation broadcasts a key invalidation to all pods via Redis Pub/Sub. This should be called whenever a cached value is updated or deleted.

func PublishInvalidationMulti added in v0.0.3

func PublishInvalidationMulti(ctx context.Context, rdb *redis.Client, keys ...string) error

PublishInvalidationMulti broadcasts multiple key invalidations

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) BLPop

func (c *HybridCache) BLPop(ctx context.Context, timeout time.Duration, keys ...string) ([]string, error)

func (*HybridCache) Close

func (c *HybridCache) Close() error

func (*HybridCache) Del

func (c *HybridCache) Del(ctx context.Context, key string) error

func (*HybridCache) Eval

func (c *HybridCache) Eval(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error)

func (*HybridCache) Exists

func (c *HybridCache) Exists(ctx context.Context, key string) (bool, error)

func (*HybridCache) Get

func (c *HybridCache) Get(ctx context.Context, key string) (string, error)

func (*HybridCache) GetOrSet

func (c *HybridCache) GetOrSet(ctx context.Context, key string, expiration time.Duration, fetch func() (string, error)) (string, error)

func (*HybridCache) GetRedisClient

func (c *HybridCache) GetRedisClient() *redis.Client

GetRedisClient attempts to return the underlying Redis client if available

func (*HybridCache) Incr

func (c *HybridCache) Incr(ctx context.Context, key string) (int64, error)

Passthrough for complex Redis commands to Remote

func (*HybridCache) LTrim

func (c *HybridCache) LTrim(ctx context.Context, key string, start, stop int64) error

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) SIsMember

func (c *HybridCache) SIsMember(ctx context.Context, key string, member interface{}) (bool, error)

func (*HybridCache) Set

func (c *HybridCache) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) 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) Stats

func (c *HybridCache) Stats(ctx context.Context) map[string]interface{}

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) BLPop

func (c *RedisCache) BLPop(ctx context.Context, timeout time.Duration, keys ...string) ([]string, error)

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) Del

func (c *RedisCache) Del(ctx context.Context, key string) error

func (*RedisCache) Eval

func (c *RedisCache) Eval(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error)

func (*RedisCache) Exists

func (c *RedisCache) Exists(ctx context.Context, key string) (bool, error)

func (*RedisCache) Get

func (c *RedisCache) Get(ctx context.Context, key string) (string, error)

func (*RedisCache) GetOrSet

func (c *RedisCache) GetOrSet(ctx context.Context, key string, expiration time.Duration, fetch func() (string, error)) (string, error)

func (*RedisCache) Incr

func (c *RedisCache) Incr(ctx context.Context, key string) (int64, error)

func (*RedisCache) LTrim

func (c *RedisCache) LTrim(ctx context.Context, key string, start, stop int64) 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

func (*RedisCache) SIsMember

func (c *RedisCache) SIsMember(ctx context.Context, key string, member interface{}) (bool, error)

func (*RedisCache) Set

func (c *RedisCache) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error

func (*RedisCache) Stats

func (c *RedisCache) Stats(ctx context.Context) map[string]interface{}

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) BLPop

func (c *RistrettoCache) BLPop(ctx context.Context, timeout time.Duration, keys ...string) ([]string, error)

func (*RistrettoCache) Close

func (c *RistrettoCache) Close() error

func (*RistrettoCache) Del

func (c *RistrettoCache) Del(ctx context.Context, key string) error

func (*RistrettoCache) Eval

func (c *RistrettoCache) Eval(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error)

func (*RistrettoCache) Exists

func (c *RistrettoCache) Exists(ctx context.Context, key string) (bool, error)

func (*RistrettoCache) Get

func (c *RistrettoCache) Get(ctx context.Context, key string) (string, 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

func (c *RistrettoCache) Incr(ctx context.Context, key string) (int64, error)

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) LTrim

func (c *RistrettoCache) LTrim(ctx context.Context, key string, start, stop int64) error

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

func (*RistrettoCache) SIsMember

func (c *RistrettoCache) SIsMember(ctx context.Context, key string, member interface{}) (bool, error)

func (*RistrettoCache) Set

func (c *RistrettoCache) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error

func (*RistrettoCache) Stats

func (c *RistrettoCache) Stats(ctx context.Context) map[string]interface{}

Jump to

Keyboard shortcuts

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