Documentation
¶
Index ¶
- Constants
- Variables
- type Cache
- type Config
- type Entry
- type MemoryConfig
- type Option
- type SetterCache
- func NewMemory[T any](client *gocache.Cache, options ...Option) SetterCache[T]
- func NewMemoryWithOptions[T any](defaultExpiration, cleanupInterval time.Duration, options ...Option) SetterCache[T]
- func NewRedis[T any](client *redis.Client, options ...Option) SetterCache[T]
- func NewRedisWithOptions[T any](opts *redis.Options, options ...Option) SetterCache[T]
Constants ¶
const ( ModeMemory = "memory" ModeRedis = "redis" ModeTwoLevel = "two-level" )
Mode represents the cache backend mode
- memory: pure in-memory
- redis: pure redis
- two-level: memory + redis chain
Variables ¶
var ErrCacheNotConfigured = errors.New("cache not configured")
ErrCacheNotConfigured is returned when trying to get from a noop cache.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[T any] = cachelib.CacheInterface[T]
func NewFromConfig ¶
NewFromConfig builds a typed cache from the given Config. Modes:
- memory: in-memory only
- redis: redis only
- two-level: memory + redis chain
Memory and Redis expiration can be configured separately. If mode is not set or invalid, returns a noop cache that does nothing.
func NewNoop ¶
NewNoop creates a new noop cache that implements the Cache interface. This is useful when cache is not configured but you want to avoid nil checks.
func NewTwoLevel ¶
func NewTwoLevel[T any](memory SetterCache[T], redis SetterCache[T]) Cache[T]
NewTwoLevel constructs a 2-level cache: memory first, then Redis. It takes already constructed setter caches so you can mix any memory/redis implementations. Typical usage: NewTwoLevel(NewMemory(...), NewRedis(...)).
type Config ¶
type Config struct {
Mode string `conf:"mode" yaml:"mode" json:"mode"`
Memory MemoryConfig `conf:"memory" yaml:"memory" json:"memory"`
Redis xredis.Config `conf:"redis" yaml:"redis" json:"redis"`
}
type Entry ¶ added in v0.9.28
type Entry[T any] struct { Value T IsEmpty bool // true if this is a negative cache entry (key not found) ExpireAt time.Time }
Entry represents a cache entry with support for negative caching. It can represent either a valid value or a "not found" marker.
func NewEmptyEntry ¶ added in v0.9.28
NewEmptyEntry creates a new negative cache entry (representing "not found").
type MemoryConfig ¶
type Option ¶
func WithExpiration ¶
type SetterCache ¶
type SetterCache[T any] = cachelib.SetterCacheInterface[T]
func NewMemory ¶
func NewMemory[T any](client *gocache.Cache, options ...Option) SetterCache[T]
NewMemory creates a pure in-memory cache using patrickmn/go-cache as the backend. Pass an existing *gocache.Cache so you control default expiration & cleanup interval. Optionally pass store options (e.g., store.WithExpiration) when setting values.
func NewMemoryWithOptions ¶
func NewMemoryWithOptions[T any](defaultExpiration, cleanupInterval time.Duration, options ...Option) SetterCache[T]
NewMemoryWithOptions is a convenience constructor that builds the patrickmn/go-cache client for you using the provided default expiration and cleanup interval.
func NewRedis ¶
func NewRedis[T any](client *redis.Client, options ...Option) SetterCache[T]
NewRedis creates a pure Redis cache using github.com/redis/go-redis/v9 as the client. You can pass store options like store.WithExpiration, store.WithTags, etc.
func NewRedisWithOptions ¶
func NewRedisWithOptions[T any](opts *redis.Options, options ...Option) SetterCache[T]
NewRedisWithOptions builds a redis.Client for you and returns the cache.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package live provides a generic in-memory cache that automatically refreshes its data periodically or on demand using polling mechanism.
|
Package live provides a generic in-memory cache that automatically refreshes its data periodically or on demand using polling mechanism. |
|
Package redis is a generated GoMock package.
|
Package redis is a generated GoMock package. |