Documentation
¶
Index ¶
- Constants
- type Config
- type DataCallback
- type ExtractKeyFunc
- type Item
- type MemoryCache
- func (c *MemoryCache[V]) Close(_ context.Context) error
- func (c *MemoryCache[V]) Delete(key string)
- func (c *MemoryCache[V]) GetOrSet(ctx context.Context, key string, dataCallback DataCallback[V]) (V, error)
- func (c *MemoryCache[V]) GetWithoutTouch(key string) (V, bool)
- func (c *MemoryCache[V]) Keys() []string
- func (c *MemoryCache[V]) Set(key string, value V)
- type RedisCache
- func (rc *RedisCache[V]) Close(_ context.Context) error
- func (rc *RedisCache[V]) Delete(ctx context.Context, key string)
- func (rc *RedisCache[V]) DeleteByPrefix(ctx context.Context, prefix string) []string
- func (rc *RedisCache[V]) GetOrSet(ctx context.Context, key string, dataCallback DataCallback[V]) (V, error)
- func (rc *RedisCache[V]) RedisClient() redis.UniversalClient
- func (rc *RedisCache[V]) RedisKey(key string) string
- func (rc *RedisCache[V]) Set(ctx context.Context, key string, value V)
- type RedisConfig
Constants ¶
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 ¶
DataCallback is a function that fetches data for a given key
type ExtractKeyFunc ¶
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]) 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.
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.