Documentation
¶
Overview ¶
Package cache provides caching with TTL support for discovery and rate limiting. Uses a Reva-style registry pattern: drivers register via init(), callers use NewDefault() or NewFromConfig().
Index ¶
- Constants
- Variables
- func RegisterDriver(name string, factory DriverFactory)
- type Cache
- type CacheWithCounter
- type Counter
- type DriverFactory
- type NoopCache
- func (n *NoopCache) Close() error
- func (n *NoopCache) Delete(_ context.Context, _ string) error
- func (n *NoopCache) Exists(_ context.Context, _ string) (bool, error)
- func (n *NoopCache) Get(_ context.Context, _ string) ([]byte, error)
- func (n *NoopCache) Set(_ context.Context, _ string, _ []byte, _ time.Duration) error
Constants ¶
const ( TTLDiscovery = 15 * time.Minute // Discovery document cache TTLRateLimit = 1 * time.Minute // Rate limit window )
Default TTLs for different cache categories.
Variables ¶
var ( // ErrNotFound reports a missing cache key. ErrNotFound = errors.New("key not found") // ErrExpired reports an expired cache entry. ErrExpired = errors.New("key expired") )
Functions ¶
func RegisterDriver ¶
func RegisterDriver(name string, factory DriverFactory)
RegisterDriver registers a cache driver by name. Called from driver init().
Types ¶
type Cache ¶
type Cache interface {
// Get retrieves a value by key. Returns ErrNotFound if not present.
Get(ctx context.Context, key string) ([]byte, error)
// Set stores a value with the given TTL. If TTL is 0, use default.
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
// Delete removes a key.
Delete(ctx context.Context, key string) error
// Exists checks if a key exists and is not expired.
Exists(ctx context.Context, key string) (bool, error)
// Close releases resources.
Close() error
}
Cache provides TTL-based key-value storage.
type CacheWithCounter ¶
CacheWithCounter combines Cache and Counter interfaces.
func NewDefault ¶
func NewDefault() CacheWithCounter
NewDefault returns the default cache (in-memory with default settings). Panics if the memory driver is not registered (caller must blank-import internal/cache/loader).
func NewFromConfig ¶
func NewFromConfig(driver string, driversConfig map[string]any) (CacheWithCounter, error)
NewFromConfig returns a cache based on the driver name and driver-specific config. If driver is empty, defaults to "memory". The driversConfig map contains per-driver configs keyed by driver name (from [cache.drivers.*]). If driversConfig is nil or missing the driver key, the driver's defaults are used. Returns an error if the driver is unknown.
type Counter ¶
type Counter interface {
// Increment adds delta to the counter and returns the new value and reset time.
// If the key doesn't exist, it's created with the given TTL.
// resetAt is the time when this window expires (used for Retry-After header).
Increment(ctx context.Context, key string, delta int64, ttl time.Duration) (count int64, resetAt time.Time, err error)
// GetCount returns the current counter value. Returns 0 if not found.
GetCount(ctx context.Context, key string) (int64, error)
// Reset sets the counter to 0.
Reset(ctx context.Context, key string) error
}
Counter provides atomic increment/decrement operations for rate limiting.
type DriverFactory ¶
type DriverFactory func(config map[string]any) CacheWithCounter
DriverFactory creates a new cache instance with driver-specific config. The config map comes from [cache.drivers.<name>] in TOML. May be nil. Factory must apply its own defaults (Reva-style ApplyDefaults). Returns CacheWithCounter so drivers can provide both key-value and counter operations.
type NoopCache ¶ added in v1.1.0
type NoopCache struct{}
NoopCache is a Cache that never stores entries. Get always returns ErrNotFound; Set, Delete, Exists, and Close are silent no-ops. Use this to disable discovery caching in tests to prevent cross-test entry leakage.
func NewNoopCache ¶ added in v1.1.0
func NewNoopCache() *NoopCache
NewNoopCache returns a Cache whose Get always misses and whose writes are discarded.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bounded provides a cardinality-bounded LRU container.
|
Package bounded provides a cardinality-bounded LRU container. |
|
Package loader registers cache drivers via blank imports.
|
Package loader registers cache drivers via blank imports. |
|
Package memory provides an in-memory cache implementation with TTL support.
|
Package memory provides an in-memory cache implementation with TTL support. |
|
Package redis provides a Redis/Valkey cache driver using valkey-go.
|
Package redis provides a Redis/Valkey cache driver using valkey-go. |