cache

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 9 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 MemoryCache

type MemoryCache[T any] 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[T any]() *MemoryCache[T]

NewMemoryCache creates a new memory cache instance.

func (*MemoryCache[T]) Close

func (m *MemoryCache[T]) Close() error

Close cleans up resources.

func (*MemoryCache[T]) Delete

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

Delete removes a key from cache.

func (*MemoryCache[T]) Get

func (m *MemoryCache[T]) Get(ctx context.Context, key string) (T, error)

Get retrieves a value from cache.

func (*MemoryCache[T]) GetWithFetch

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

GetWithFetch retrieves a value using the cache-aside pattern. On cache miss, fetchFunc is called and the result is stored in cache. No stampede protection is provided (single-instance memory cache).

func (*MemoryCache[T]) Health

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

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

func (*MemoryCache[T]) Set

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

Set stores a value in cache with TTL.

type RueidisAsideCache

type RueidisAsideCache[T any] 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[T any](
	ctx context.Context,
	addr, password string,
	db int,
	keyPrefix string,
	clientTTL time.Duration,
	cacheSizeMB int,
) (*RueidisAsideCache[T], 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[T]) Get

func (r *RueidisAsideCache[T]) Get(ctx context.Context, key string) (T, error)

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

func (*RueidisAsideCache[T]) GetWithFetch

func (r *RueidisAsideCache[T]) GetWithFetch(
	ctx context.Context,
	key string,
	ttl time.Duration,
	fetchFunc func(ctx context.Context, key string) (T, error),
) (T, 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) Set

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

Set stores a value in Redis with TTL.

type RueidisCache

type RueidisCache[T any] 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[T any](
	ctx context.Context,
	addr, password string,
	db int,
	keyPrefix string,
) (*RueidisCache[T], 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[T]) Get

func (r *RueidisCache[T]) Get(ctx context.Context, key string) (T, error)

Get retrieves a value from Redis.

func (*RueidisCache[T]) GetWithFetch

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

GetWithFetch retrieves a value using the cache-aside pattern. On cache miss, fetchFunc is called and the result is stored in cache. No stampede protection is provided.

func (*RueidisCache) Health

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

Health checks if Redis is reachable.

func (*RueidisCache) Set

func (r *RueidisCache) Set(ctx context.Context, key string, value T, 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