cache

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCacheMiss indicates the requested key was not found in cache
	ErrCacheMiss = errors.New("cache: key not found")

	// ErrCacheUnavailable indicates the cache backend is unavailable
	ErrCacheUnavailable = errors.New("cache: backend unavailable")

	// ErrInvalidValue indicates the cached value cannot be parsed
	ErrInvalidValue = errors.New("cache: invalid value")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get retrieves a single value from cache
	Get(ctx context.Context, key string) (int64, error)

	// Set stores a single value in cache with TTL
	Set(ctx context.Context, key string, value int64, ttl time.Duration) error

	// GetWithFetch retrieves a value using cache-aside pattern.
	// On cache miss, calls fetchFunc to get the value and automatically stores it in cache.
	// This method is part of the main interface to ensure all implementations provide
	// optimal cache-aside support. RueidisAsideCache provides an optimized implementation
	// using rueidisaside's automatic cache management.
	GetWithFetch(
		ctx context.Context,
		key string,
		ttl time.Duration,
		fetchFunc func(ctx context.Context, key string) (int64, error),
	) (int64, error)

	// MGet retrieves multiple values from cache
	// Returns a map of key->value for keys that exist
	MGet(ctx context.Context, keys []string) (map[string]int64, error)

	// MSet stores multiple values in cache with TTL
	MSet(ctx context.Context, values map[string]int64, ttl time.Duration) error

	// Delete removes a key from cache
	Delete(ctx context.Context, key string) error

	// Close closes the cache connection
	Close() error

	// Health checks if the cache is healthy
	Health(ctx context.Context) error
}

Cache defines the interface for caching metrics data. All implementations should store int64 values (counts) for simplicity.

type MemoryCache

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

MemoryCache implements Cache interface with in-memory storage. Uses lazy expiration (checks expiry on Get). Suitable for single-instance deployments.

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache creates a new memory cache instance.

func (*MemoryCache) Close

func (m *MemoryCache) Close() error

Close cleans up resources.

func (*MemoryCache) Delete

func (m *MemoryCache) Delete(ctx context.Context, key string) error

Delete removes a key from cache.

func (*MemoryCache) Get

func (m *MemoryCache) Get(ctx context.Context, key string) (int64, error)

Get retrieves a value from cache.

func (*MemoryCache) GetWithFetch

func (m *MemoryCache) GetWithFetch(
	ctx context.Context,
	key string,
	ttl time.Duration,
	fetchFunc func(ctx context.Context, key string) (int64, error),
) (int64, error)

GetWithFetch retrieves a value using cache-aside pattern. On cache miss, calls fetchFunc to get the value and stores it in cache.

func (*MemoryCache) Health

func (m *MemoryCache) Health(ctx context.Context) error

Health checks if the cache is healthy (always true for memory cache).

func (*MemoryCache) MGet

func (m *MemoryCache) MGet(ctx context.Context, keys []string) (map[string]int64, error)

MGet retrieves multiple values from cache.

func (*MemoryCache) MSet

func (m *MemoryCache) MSet(ctx context.Context, values map[string]int64, ttl time.Duration) error

MSet stores multiple values in cache with TTL.

func (*MemoryCache) Set

func (m *MemoryCache) Set(ctx context.Context, key string, value int64, ttl time.Duration) error

Set stores a value in cache with TTL.

type RueidisAsideCache

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

RueidisAsideCache implements Cache interface using rueidisaside for cache-aside pattern. Uses rueidis' automatic client-side caching with RESP3 protocol for cache invalidation. Suitable for high-load multi-instance deployments (5+ pods).

func NewRueidisAsideCache

func NewRueidisAsideCache(
	ctx context.Context,
	addr, password string,
	db int,
	keyPrefix string,
	clientTTL time.Duration,
	cacheSizeMB int,
) (*RueidisAsideCache, error)

NewRueidisAsideCache creates a new Redis cache with client-side caching using rueidisaside. clientTTL is the local cache TTL (e.g., 30s). Redis will automatically invalidate the local cache when keys change. cacheSizeMB is the client-side cache size per connection in megabytes. Note: Rueidis uses connection pooling (typically ~10 connections based on GOMAXPROCS), so total memory usage will be cacheSizeMB * number_of_connections.

func (*RueidisAsideCache) Close

func (r *RueidisAsideCache) Close() error

Close closes the Redis connection.

func (*RueidisAsideCache) Delete

func (r *RueidisAsideCache) Delete(ctx context.Context, key string) error

Delete removes a key from Redis.

func (*RueidisAsideCache) Get

func (r *RueidisAsideCache) Get(ctx context.Context, key string) (int64, error)

Get retrieves a value from Redis with client-side caching. Uses DoCache to leverage RESP3 client-side caching with automatic invalidation.

func (*RueidisAsideCache) GetWithFetch

func (r *RueidisAsideCache) GetWithFetch(
	ctx context.Context,
	key string,
	ttl time.Duration,
	fetchFunc func(ctx context.Context, key string) (int64, error),
) (int64, error)

GetWithFetch retrieves a value using rueidisaside's cache-aside pattern. This is an enhanced method that leverages rueidisaside's automatic cache management. The fetchFunc is called automatically on cache miss to populate the cache.

func (*RueidisAsideCache) Health

func (r *RueidisAsideCache) Health(ctx context.Context) error

Health checks if Redis is reachable.

func (*RueidisAsideCache) MGet

func (r *RueidisAsideCache) MGet(ctx context.Context, keys []string) (map[string]int64, error)

MGet retrieves multiple values from Redis with client-side caching.

func (*RueidisAsideCache) MSet

func (r *RueidisAsideCache) MSet(
	ctx context.Context,
	values map[string]int64,
	ttl time.Duration,
) error

MSet stores multiple values in Redis with TTL.

func (*RueidisAsideCache) Set

func (r *RueidisAsideCache) Set(
	ctx context.Context,
	key string,
	value int64,
	ttl time.Duration,
) error

Set stores a value in Redis with TTL.

type RueidisCache

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

RueidisCache implements Cache interface using Redis via rueidis client. Suitable for multi-instance deployments where cache needs to be shared.

func NewRueidisCache

func NewRueidisCache(
	ctx context.Context,
	addr, password string,
	db int,
	keyPrefix string,
) (*RueidisCache, error)

NewRueidisCache creates a new Redis cache instance using rueidis.

func (*RueidisCache) Close

func (r *RueidisCache) Close() error

Close closes the Redis connection.

func (*RueidisCache) Delete

func (r *RueidisCache) Delete(ctx context.Context, key string) error

Delete removes a key from Redis.

func (*RueidisCache) Get

func (r *RueidisCache) Get(ctx context.Context, key string) (int64, error)

Get retrieves a value from Redis.

func (*RueidisCache) GetWithFetch

func (r *RueidisCache) GetWithFetch(
	ctx context.Context,
	key string,
	ttl time.Duration,
	fetchFunc func(ctx context.Context, key string) (int64, error),
) (int64, error)

GetWithFetch retrieves a value using cache-aside pattern. On cache miss, calls fetchFunc to get the value and stores it in cache.

func (*RueidisCache) Health

func (r *RueidisCache) Health(ctx context.Context) error

Health checks if Redis is reachable.

func (*RueidisCache) MGet

func (r *RueidisCache) MGet(ctx context.Context, keys []string) (map[string]int64, error)

MGet retrieves multiple values from Redis.

func (*RueidisCache) MSet

func (r *RueidisCache) MSet(ctx context.Context, values map[string]int64, ttl time.Duration) error

MSet stores multiple values in Redis with TTL.

func (*RueidisCache) Set

func (r *RueidisCache) Set(ctx context.Context, key string, value int64, ttl time.Duration) error

Set stores a value in Redis with TTL.

Jump to

Keyboard shortcuts

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