Documentation
¶
Overview ¶
Package cache defines deployment-independent cache contracts and the building blocks for typed, multi-backend cache operations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCacheNotFound is the normalized cache-miss error. ErrCacheNotFound = errors.New("cache item not found") // ErrNoCache indicates that caching is disabled for the operation. ErrNoCache = errors.New("cache is disabled") )
Functions ¶
This section is empty.
Types ¶
type BaseCache ¶
type BaseCache interface {
Get(ctx context.Context, key string) ([]byte, error)
Save(ctx context.Context, key string, value []byte, expire time.Duration) error
Delete(ctx context.Context, key string) error
}
BaseCache is a byte-oriented cache backend operation interface.
type Cache ¶
type Cache[T any] interface { Get(ctx context.Context, key string, optFns ...CacheOption) (T, error) Save(ctx context.Context, key string, entity T, expire time.Duration, optFns ...CacheOption) error Delete(ctx context.Context, key string, optFns ...CacheOption) error GetCacheManager() CacheManager }
Cache is a typed cache operation interface. Backend selection and expiration are controlled by CacheOption values.
type CacheManager ¶
type CacheManager interface {
GetRedis(ctx context.Context) RedisBaseCache
GetLocal(ctx context.Context) BaseCache
GetLocalOnRedis(ctx context.Context) BaseCache
GetNoCache(ctx context.Context) BaseCache
}
CacheManager provides access to the available cache backends.
type CacheOption ¶
type CacheOption func(option *CacheOptions)
CacheOption configures a CacheOptions value.
func UseCustomCache ¶
func UseCustomCache(bc BaseCache) CacheOption
UseCustomCache uses a caller-provided BaseCache, mainly for tests.
func UseLocalOnRedis ¶
func UseLocalOnRedis() CacheOption
UseLocalOnRedis selects the two-level local-on-Redis backend.
type CacheOptions ¶
CacheOptions carries backend selection for one cache operation.
func MergeCacheOptions ¶
func MergeCacheOptions(options ...CacheOption) *CacheOptions
MergeCacheOptions merges options into a CacheOptions value with Redis as the default backend.
type CacheType ¶
type CacheType int32
CacheType selects a cache backend.
const ( // NoCache disables caching. NoCache CacheType = 0 // RedisCache uses the Redis backend. RedisCache CacheType = 1 // LocalCache uses the local bigcache backend. LocalCache CacheType = 2 // LocalOnRedisLevel uses the two-level local-on-Redis backend. LocalOnRedisLevel CacheType = 3 // CustomCache uses a caller-provided BaseCache, mainly for tests. CustomCache CacheType = 4 )
type Config ¶
type Config struct {
// RedisPrefix is prepended to every Redis cache key.
RedisPrefix string
// LocalPrefix is prepended to every standalone local cache key.
LocalPrefix string
}
Config contains backend-independent cache configuration.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns cache configuration with no key prefixes.
type Incrementer ¶
type Incrementer interface {
IncrByExpire(ctx context.Context, key string, value int64, expiration time.Duration) (int64, error)
}
Incrementer is implemented by backends that support atomic increment with expiration, currently the Redis backend.
type LocalStore ¶
type LocalStore interface {
Get(key string) ([]byte, error)
Set(key string, value []byte) error
Delete(key string) error
}
LocalStore is the minimal local cache surface used by the cache component. bigcache.BigCache satisfies it directly.
type RedisBaseCache ¶
type RedisBaseCache interface {
BaseCache
Incrementer
}
RedisBaseCache is the Redis backend surface: generic byte operations plus atomic increment with expiration.
Directories
¶
| Path | Synopsis |
|---|---|
|
operation
|
|
|
cachemanager
Package cachemanager provides typed cache operations and the concrete CacheManager implementation.
|
Package cachemanager provides typed cache operations and the concrete CacheManager implementation. |
|
local
Package local provides a bigcache-backed cache.BaseCache implementation.
|
Package local provides a bigcache-backed cache.BaseCache implementation. |
|
local_on_redis
Package localonredis provides a two-level cache that reads local first and falls back to Redis, backfilling the local cache on a Redis hit.
|
Package localonredis provides a two-level cache that reads local first and falls back to Redis, backfilling the local cache on a Redis hit. |
|
no_cache
Package nocache provides a no-op cache.BaseCache implementation.
|
Package nocache provides a no-op cache.BaseCache implementation. |
|
redis
Package redis provides a Redis-backed cache.BaseCache implementation on top of the deployment-independent *redis.Client contract.
|
Package redis provides a Redis-backed cache.BaseCache implementation on top of the deployment-independent *redis.Client contract. |
|
Package testhelper provides test helpers for the cache component.
|
Package testhelper provides test helpers for the cache component. |