cache

package
v0.92.1 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package cache provides caching abstractions and implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDefaultRedisStore added in v0.92.0

func SetDefaultRedisStore(s *RedisStore)

SetDefaultRedisStore sets the global Redis store for health checks and other cross-package access. Called once during server initialization.

Types

type Cache added in v0.31.1

type Cache struct {
	// contains filtered or unexported fields
}
var Instance *Cache

func NewCache added in v0.31.1

func NewCache(_ *config.Type) (*Cache, error)

func (*Cache) Del added in v0.31.1

func (c *Cache) Del(_ context.Context, key Key) error

Del removes a key from the cache.

func (*Cache) DelByPrefix added in v0.92.0

func (c *Cache) DelByPrefix(capType string)

DelByPrefix removes all cached keys registered under the given capability prefix. The prefix corresponds to a capability type string (e.g. "bookmark", "kanban"). If the prefix is empty, this is a no-op.

func (*Cache) DelRaw added in v0.92.0

func (c *Cache) DelRaw(key string)

func (*Cache) Exists added in v0.92.0

func (c *Cache) Exists(_ context.Context, key Key) (bool, error)

Exists checks whether a key is present in the cache.

func (*Cache) Expire added in v0.92.0

func (c *Cache) Expire(_ context.Context, key Key, ttl TTL) error

Expire refreshes the TTL on an existing key.

func (*Cache) Get added in v0.31.1

func (c *Cache) Get(_ context.Context, key Key) (string, bool, error)

Get retrieves a string value from the cache. Returns false if the key is not found.

func (*Cache) GetBytes added in v0.92.0

func (c *Cache) GetBytes(key string) ([]byte, bool)

GetBytes retrieves raw bytes from the cache. Returns false if the key is not found or the value is not a byte slice.

func (*Cache) GetRaw added in v0.92.0

func (c *Cache) GetRaw(key string) (any, bool)

func (*Cache) Set added in v0.31.1

func (c *Cache) Set(_ context.Context, key Key, value string, ttl TTL) error

Set stores a string value with the given TTL.

func (*Cache) SetNX added in v0.92.0

func (c *Cache) SetNX(_ context.Context, key Key, value string, ttl TTL) (bool, error)

SetNX stores a value only if the key does not already exist. Returns true if the value was set.

func (*Cache) SetRaw added in v0.92.0

func (c *Cache) SetRaw(key string, value any, cost int64) bool

func (*Cache) SetWithTTL added in v0.31.1

func (c *Cache) SetWithTTL(key string, value any, cost int64, ttl time.Duration) bool

func (*Cache) SetWithTTLCap added in v0.92.0

func (c *Cache) SetWithTTLCap(key string, value []byte, cost int64, ttl time.Duration, capType string) bool

SetWithTTLCap stores a byte value with TTL and registers the key under the given capability prefix for later prefix-based invalidation via DelByPrefix.

func (*Cache) Wait added in v0.31.1

func (c *Cache) Wait()

type IntCache added in v0.92.0

type IntCache interface {
	// GetInt64 retrieves the integer value for key.
	GetInt64(ctx context.Context, key Key) (int64, error)
	// SetInt64 stores an integer value with the given TTL.
	SetInt64(ctx context.Context, key Key, value int64, ttl TTL) error
	// Incr atomically increments the key and returns the new value.
	Incr(ctx context.Context, key Key) (int64, error)
	// IncrWithTTL atomically increments the key, sets the TTL if the key is new,
	// and returns the new value.
	IncrWithTTL(ctx context.Context, key Key, ttl TTL) (int64, error)
}

IntCache covers integer counters, backed by Redis. It provides atomic increment operations suitable for rate limiting, statistics, and gauges.

type Key added in v0.92.0

type Key struct {
	// Prefix identifies the business domain (e.g. "online", "chat", "notify").
	Prefix string
	// Entity identifies the data purpose within the domain (e.g. "agent", "session", "throttle").
	Entity string
	// Identifier is the business primary key, which may contain additional colon-delimited segments.
	Identifier string
}

Key represents a namespaced cache key following the format {prefix}:{entity}:{identifier}. It enforces consistent key naming across all cache operations.

func NewKey added in v0.92.0

func NewKey(prefix, entity, identifier string) Key

NewKey creates a Key from its components.

func (Key) String added in v0.92.0

func (k Key) String() string

String returns the colon-delimited key string used for cache operations.

type ListCache added in v0.92.0

type ListCache interface {
	// Push appends values to the tail of the list. Returns the length after push.
	Push(ctx context.Context, key Key, values ...string) (int64, error)
	// Range returns elements from start to stop (inclusive, zero-indexed).
	// Use -1 for stop to read to the end.
	Range(ctx context.Context, key Key, start, stop int64) ([]string, error)
	// Len returns the current length of the list.
	Len(ctx context.Context, key Key) (int64, error)
	// Clear removes all elements from the list.
	Clear(ctx context.Context, key Key) error
}

ListCache covers list-based aggregation buffers, backed by Redis. It provides ordered insertion and ranged retrieval suitable for message queues and event buffers.

type RedisStore added in v0.92.0

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

RedisStore wraps a Redis client to implement StringCache, IntCache, SetCache, and ListCache. All operations record cache hit/miss/eviction metrics via the helpers in metrics.go.

func DefaultRedisStore added in v0.92.0

func DefaultRedisStore() *RedisStore

DefaultRedisStore returns the global Redis store. May return nil before initialization.

func NewRedisStore added in v0.92.0

func NewRedisStore(client *redis.Client) *RedisStore

NewRedisStore creates a RedisStore backed by a Redis client.

func (*RedisStore) Add added in v0.92.0

func (s *RedisStore) Add(ctx context.Context, key Key, ttl TTL, members ...string) (int64, error)

Add adds members to a set. If the key is newly created, sets the TTL.

func (*RedisStore) Clear added in v0.92.0

func (s *RedisStore) Clear(ctx context.Context, key Key) error

Clear removes the entire set or list key.

func (*RedisStore) Del added in v0.92.0

func (s *RedisStore) Del(ctx context.Context, key Key) error

Del removes a key from the cache.

func (*RedisStore) Exists added in v0.92.0

func (s *RedisStore) Exists(ctx context.Context, key Key) (bool, error)

Exists checks whether a key is present.

func (*RedisStore) ExistsRaw added in v0.92.0

func (s *RedisStore) ExistsRaw(ctx context.Context, key string) (bool, error)

ExistsRaw checks whether a raw key string exists in Redis. Used for scanning and other operations where a pre-formatted key string is available.

func (*RedisStore) Expire added in v0.92.0

func (s *RedisStore) Expire(ctx context.Context, key Key, ttl TTL) error

Expire refreshes the TTL on an existing key.

func (*RedisStore) Get added in v0.92.0

func (s *RedisStore) Get(ctx context.Context, key Key) (string, bool, error)

Get retrieves a string value by key. Returns false if the key is not found.

func (*RedisStore) GetInt64 added in v0.92.0

func (s *RedisStore) GetInt64(ctx context.Context, key Key) (int64, error)

GetInt64 retrieves an int64 value. Returns 0 if the key is not found.

func (*RedisStore) GetMetricsInt64 added in v0.92.0

func (s *RedisStore) GetMetricsInt64(key string) int64

GetMetricsInt64 retrieves a named metric value.

func (*RedisStore) Incr added in v0.92.0

func (s *RedisStore) Incr(ctx context.Context, key Key) (int64, error)

Incr atomically increments the integer at key by 1 and returns the new value.

func (*RedisStore) IncrWithTTL added in v0.92.0

func (s *RedisStore) IncrWithTTL(ctx context.Context, key Key, ttl TTL) (int64, error)

IncrWithTTL atomically increments the integer at key by 1 and sets the TTL if the key was newly created (i.e., the new count is 1).

func (*RedisStore) IsMember added in v0.92.0

func (s *RedisStore) IsMember(ctx context.Context, key Key, member string) (bool, error)

IsMember checks whether a member exists in the set.

func (*RedisStore) Len added in v0.92.0

func (s *RedisStore) Len(ctx context.Context, key Key) (int64, error)

Len returns the length of the list.

func (*RedisStore) Members added in v0.92.0

func (s *RedisStore) Members(ctx context.Context, key Key) ([]string, error)

Members returns all members of the set.

func (*RedisStore) Ping added in v0.92.0

func (s *RedisStore) Ping(ctx context.Context) (time.Duration, error)

Ping checks Redis connectivity and returns the round-trip latency.

func (*RedisStore) Push added in v0.92.0

func (s *RedisStore) Push(ctx context.Context, key Key, values ...string) (int64, error)

Push appends values to the right end of a list. Returns the list length after the push.

func (*RedisStore) Range added in v0.92.0

func (s *RedisStore) Range(ctx context.Context, key Key, start, stop int64) ([]string, error)

Range returns a range of elements from the list. Use 0, -1 for all elements.

func (*RedisStore) Remove added in v0.92.0

func (s *RedisStore) Remove(ctx context.Context, key Key, members ...string) (int64, error)

Remove removes members from a set.

func (*RedisStore) ScanKeys added in v0.92.0

func (s *RedisStore) ScanKeys(ctx context.Context, pattern string, count int64) ([]string, error)

ScanKeys scans for keys matching a pattern, returning all matching keys.

func (*RedisStore) Set added in v0.92.0

func (s *RedisStore) Set(ctx context.Context, key Key, value string, ttl TTL) error

Set stores a string value with the given TTL.

func (*RedisStore) SetInt64 added in v0.92.0

func (s *RedisStore) SetInt64(ctx context.Context, key Key, value int64, ttl TTL) error

SetInt64 stores an int64 value with the given TTL.

func (*RedisStore) SetMetricsInt64 added in v0.92.0

func (s *RedisStore) SetMetricsInt64(key string, value int64)

SetMetricsInt64 stores a named metric value. Convenience wrapper around SetInt64.

func (*RedisStore) SetNX added in v0.92.0

func (s *RedisStore) SetNX(ctx context.Context, key Key, value string, ttl TTL) (bool, error)

SetNX sets a key only if it does not already exist. Returns true if set, false if key already existed.

type SetCache added in v0.92.0

type SetCache interface {
	// Add adds one or more members to the set. Returns the number of members added.
	Add(ctx context.Context, key Key, ttl TTL, members ...string) (int64, error)
	// IsMember reports whether member is in the set.
	IsMember(ctx context.Context, key Key, member string) (bool, error)
	// Members returns all members of the set.
	Members(ctx context.Context, key Key) ([]string, error)
	// Remove removes one or more members from the set. Returns the number removed.
	Remove(ctx context.Context, key Key, members ...string) (int64, error)
	// Clear removes all members from the set.
	Clear(ctx context.Context, key Key) error
}

SetCache covers set-based deduplication, backed by Redis. It manages unordered collections of unique members with O(1) membership tests.

type StringCache added in v0.92.0

type StringCache interface {
	// Get retrieves the string value for key. Returns false if not present.
	Get(ctx context.Context, key Key) (string, bool, error)
	// Set stores a string value with the given TTL.
	Set(ctx context.Context, key Key, value string, ttl TTL) error
	// SetNX sets the value only if the key does not already exist.
	// Returns true if the value was set.
	SetNX(ctx context.Context, key Key, value string, ttl TTL) (bool, error)
	// Del removes the key from the cache.
	Del(ctx context.Context, key Key) error
	// Exists returns whether the key is present in the cache.
	Exists(ctx context.Context, key Key) (bool, error)
	// Expire sets or updates the TTL on an existing key.
	Expire(ctx context.Context, key Key, ttl TTL) error
}

StringCache covers raw string KV operations, backed by either Ristretto or Redis. It is the foundational interface that every backend must satisfy.

type TTL added in v0.92.0

type TTL time.Duration

TTL represents a cache time-to-live duration. It wraps time.Duration to provide named constants for common cache expiry policies.

const (
	// TTLNone disables expiry — items persist until explicitly deleted or evicted.
	TTLNone TTL = 0
	// TTLMinute is a 1-minute lifetime, suitable for transient locks or status flags.
	TTLMinute TTL = TTL(time.Minute)
	// TTLShort is a 2-minute lifetime, used for heartbeat and liveness signals.
	TTLShort TTL = TTL(2 * time.Minute)
	// TTLMedium is a 10-minute lifetime, used for temporary state with moderate freshness requirements.
	TTLMedium TTL = TTL(10 * time.Minute)
	// TTLLong is a 1-hour lifetime, used for cached data that can tolerate slight staleness.
	TTLLong TTL = TTL(1 * time.Hour)
	// TTLSession is a 24-hour lifetime, used for chat sessions and user presence.
	TTLSession TTL = TTL(24 * time.Hour)
	// TTLDay is a 24-hour lifetime, used for daily deduplication windows.
	TTLDay TTL = TTL(24 * time.Hour)
	// TTLWeek is a 7-day lifetime, used for medium-term deduplication.
	TTLWeek TTL = TTL(7 * 24 * time.Hour)
	// TTLMonth is a 30-day lifetime, used for long-term deduplication to prevent unbounded key growth.
	TTLMonth TTL = TTL(30 * 24 * time.Hour)
)

Predefined TTL constants for standardized cache expiry policies.

func (TTL) Duration added in v0.92.0

func (t TTL) Duration() time.Duration

Duration returns the TTL as a standard time.Duration for use with cache backends.

Jump to

Keyboard shortcuts

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