Documentation
¶
Index ¶
- Variables
- type Cache
- type MemoryCache
- func (m *MemoryCache) Close() error
- func (m *MemoryCache) Delete(ctx context.Context, key string) error
- func (m *MemoryCache) Get(ctx context.Context, key string) (int64, error)
- func (m *MemoryCache) GetWithFetch(ctx context.Context, key string, ttl time.Duration, ...) (int64, error)
- func (m *MemoryCache) Health(ctx context.Context) error
- func (m *MemoryCache) MGet(ctx context.Context, keys []string) (map[string]int64, error)
- func (m *MemoryCache) MSet(ctx context.Context, values map[string]int64, ttl time.Duration) error
- func (m *MemoryCache) Set(ctx context.Context, key string, value int64, ttl time.Duration) error
- type RueidisAsideCache
- func (r *RueidisAsideCache) Close() error
- func (r *RueidisAsideCache) Delete(ctx context.Context, key string) error
- func (r *RueidisAsideCache) Get(ctx context.Context, key string) (int64, error)
- func (r *RueidisAsideCache) GetWithFetch(ctx context.Context, key string, ttl time.Duration, ...) (int64, error)
- func (r *RueidisAsideCache) Health(ctx context.Context) error
- func (r *RueidisAsideCache) MGet(ctx context.Context, keys []string) (map[string]int64, error)
- func (r *RueidisAsideCache) MSet(ctx context.Context, values map[string]int64, ttl time.Duration) error
- func (r *RueidisAsideCache) Set(ctx context.Context, key string, value int64, ttl time.Duration) error
- type RueidisCache
- func (r *RueidisCache) Close() error
- func (r *RueidisCache) Delete(ctx context.Context, key string) error
- func (r *RueidisCache) Get(ctx context.Context, key string) (int64, error)
- func (r *RueidisCache) GetWithFetch(ctx context.Context, key string, ttl time.Duration, ...) (int64, error)
- func (r *RueidisCache) Health(ctx context.Context) error
- func (r *RueidisCache) MGet(ctx context.Context, keys []string) (map[string]int64, error)
- func (r *RueidisCache) MSet(ctx context.Context, values map[string]int64, ttl time.Duration) error
- func (r *RueidisCache) Set(ctx context.Context, key string, value int64, ttl time.Duration) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCacheMiss indicates the requested key was not found in cache ErrCacheMiss = errors.New("cache: key not found") 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) Delete ¶
func (m *MemoryCache) Delete(ctx context.Context, key string) error
Delete removes a key 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).
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 ¶
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 ¶
MGet retrieves multiple values from Redis with client-side caching.
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) 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.