cache

package
v0.0.0-...-6c76104 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Index

Constants

View Source
const (
	RedisRefreshIntervalOff = 0
	RedisLockOff            = -1
	RedisDefaultTimeout     = 2 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config[V any] struct {
	// TTL is the time-to-live for cache entries
	TTL time.Duration
	// RefreshInterval is the interval at which cache entries are refreshed in the background
	// If 0, no background refresh is performed
	RefreshInterval time.Duration
	// CallbackTimeout is the timeout for the data callback function
	// Defaults to 30 seconds if not specified
	CallbackTimeout time.Duration
	// RefreshTimeout is the timeout for refresh operations
	// Defaults to 30 seconds if not specified
	RefreshTimeout time.Duration
	// ExtractKeyFunc is an optional function to extract a key from the value
	ExtractKeyFunc ExtractKeyFunc[V]
}

Config holds the configuration for a Cache

type DataCallback

type DataCallback[V any] func(ctx context.Context, key string) (V, error)

DataCallback is a function that fetches data for a given key

type ExtractKeyFunc

type ExtractKeyFunc[V any] func(value V) string

ExtractKeyFunc is a function that extracts a key from a value

type Item

type Item[V any] struct {
	// contains filtered or unexported fields
}

Item wraps a cached value with metadata for refresh tracking

type MemoryCache

type MemoryCache[V any] struct {
	// contains filtered or unexported fields
}

MemoryCache is a generic cache with optional background refresh support

func NewMemoryCache

func NewMemoryCache[V any](config Config[V]) *MemoryCache[V]

NewMemoryCache creates a new Cache with the given configuration

func (*MemoryCache[V]) Close

func (c *MemoryCache[V]) Close(_ context.Context) error

func (*MemoryCache[V]) Delete

func (c *MemoryCache[V]) Delete(key string)

Delete removes a value from the cache

func (*MemoryCache[V]) GetOrSet

func (c *MemoryCache[V]) GetOrSet(ctx context.Context, key string, dataCallback DataCallback[V]) (V, error)

GetOrSet retrieves a value from the cache, or fetches it using the callback if not present If RefreshInterval is configured and the cached value is older than the interval, it triggers a background refresh while returning the stale value

func (*MemoryCache[V]) GetWithoutTouch

func (c *MemoryCache[V]) GetWithoutTouch(key string) (V, bool)

GetWithoutTouch retrieves a value from the cache by key

func (*MemoryCache[V]) Keys

func (c *MemoryCache[V]) Keys() []string

func (*MemoryCache[V]) Set

func (c *MemoryCache[V]) Set(key string, value V)

Set stores a value in the cache with the default TTL

type RedisCache

type RedisCache[V any] struct {
	// contains filtered or unexported fields
}

RedisCache is a generic two-tier cache: Redis + user callback.

func NewRedisCache

func NewRedisCache[V any](config RedisConfig[V]) *RedisCache[V]

NewRedisCache creates a new RedisCache with the given configuration.

func (*RedisCache[V]) Close

func (rc *RedisCache[V]) Close(_ context.Context) error

Close is a no-op (no background goroutines to stop).

func (*RedisCache[V]) Delete

func (rc *RedisCache[V]) Delete(ctx context.Context, key string)

Delete removes a value from Redis.

func (*RedisCache[V]) DeleteByPrefix

func (rc *RedisCache[V]) DeleteByPrefix(ctx context.Context, prefix string) []string

DeleteByPrefix removes all keys matching the given prefix from Redis. Uses SCAN (not KEYS) to avoid blocking Redis on large keyspaces. Keys are collected first, then deleted after SCAN completes so that the keyspace is not mutated during cursor iteration. Returns the list of deleted cache keys (without the Redis prefix).

func (*RedisCache[V]) GetOrSet

func (rc *RedisCache[V]) GetOrSet(ctx context.Context, key string, dataCallback DataCallback[V]) (V, error)

GetOrSet retrieves a value from Redis, falling back to dataCallback on miss. On callback hit, the value is backfilled into Redis. Singleflight deduplicates concurrent misses for the same key.

func (*RedisCache[V]) RedisClient

func (rc *RedisCache[V]) RedisClient() redis.UniversalClient

RedisClient returns the underlying Redis client for custom operations (e.g. Lua scripts).

func (*RedisCache[V]) RedisKey

func (rc *RedisCache[V]) RedisKey(key string) string

RedisKey returns the full Redis key for a given cache key.

func (*RedisCache[V]) Set

func (rc *RedisCache[V]) Set(ctx context.Context, key string, value V)

Set stores a value in Redis.

type RedisConfig

type RedisConfig[V any] struct {
	RedisClient redis.UniversalClient
	TTL         time.Duration
	// RefreshInterval triggers a background refresh of a Redis entry
	// when its age exceeds this duration. 0 = no background refresh.
	RefreshInterval time.Duration
	// RefreshTimeout is the timeout for the callback during Redis refresh.
	// Defaults to 30s.
	RefreshTimeout time.Duration
	RedisTimeout   time.Duration // default 2s
	RedisPrefix    string        // e.g. "template:build"
	// ExtractKeyFunc is an optional function to extract a key from the value.
	// When set, the key used for Redis storage is derived from the fetched value
	// rather than the original lookup key.
	ExtractKeyFunc ExtractKeyFunc[V]
	// LockTTL controls distributed locking for cache updates.
	// Lock is acquired before calling the data callback to prevent thundering herd across instances.
	// TTL is auto-computed as RefreshTimeout + 2*RedisTimeout
	LockTTL time.Duration
	// LockRetryInterval is the interval between lock acquisition retries.
	// Defaults to 50ms.
	LockRetryInterval time.Duration
}

RedisConfig holds the configuration for a RedisCache.

Jump to

Keyboard shortcuts

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