Documentation
¶
Overview ¶
Package cache provides a backend-neutral cache abstraction with explicit factory registration and an in-memory backend for the default provider name.
Index ¶
- Constants
- func RegisterFile(reg *FactoryRegistry) error
- func RegisterMemory(reg *FactoryRegistry) error
- type BatchStore
- type CloseStore
- type Component
- type Config
- type ConfigTypeError
- type Factory
- type FactoryRegistry
- type FileConfig
- type FileStore
- func (s *FileStore) CleanupExpired(ctx context.Context, maxEntries int) (int, error)
- func (s *FileStore) Delete(ctx context.Context, key string) error
- func (s *FileStore) Exists(ctx context.Context, key string) (bool, error)
- func (s *FileStore) Get(ctx context.Context, key string) (value []byte, found bool, err error)
- func (s *FileStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- type MemoryConfig
- type MemoryStore
- func (s *MemoryStore) Delete(_ context.Context, key string) error
- func (s *MemoryStore) Exists(ctx context.Context, key string) (bool, error)
- func (s *MemoryStore) Get(ctx context.Context, key string) (value []byte, found bool, err error)
- func (s *MemoryStore) GetMany(ctx context.Context, keys []string) (map[string][]byte, error)
- func (s *MemoryStore) Set(_ context.Context, key string, value []byte, ttl time.Duration) error
- type Store
- type TypedStore
Constants ¶
const ( // ProviderMemory is the lean in-process cache backend shipped by core. ProviderMemory = "memory" // ProviderRedis is registered by the opt-in cache/redis adapter module. ProviderRedis = "redis" // ProviderFile is the filesystem-backed cache shipped by core. ProviderFile = "fs" DefaultProvider = ProviderMemory )
Variables ¶
This section is empty.
Functions ¶
func RegisterFile ¶
func RegisterFile(reg *FactoryRegistry) error
RegisterFile registers the filesystem backend into an explicit registry.
func RegisterMemory ¶
func RegisterMemory(reg *FactoryRegistry) error
RegisterMemory registers the core memory backend into an explicit registry.
Types ¶
type BatchStore ¶
type BatchStore interface {
GetMany(ctx context.Context, keys []string) (map[string][]byte, error)
}
BatchStore is optionally implemented by stores that support efficient batch reads.
type CloseStore ¶
type CloseStore interface {
Close() error
}
CloseStore is optionally implemented by stores that hold resources.
type Component ¶
type Component struct {
// contains filtered or unexported fields
}
Component wraps Store and implements component.Component.
func NewComponent ¶
func NewComponent(registry *FactoryRegistry, cfg Config, providerCfg any, log *logging.Logger) *Component
NewComponent creates a cache component. The registry is explicit and must contain the selected provider.
func (*Component) Describe ¶
func (c *Component) Describe() component.Description
type Config ¶
type Config struct {
Name string `mapstructure:"name" json:"name" yaml:"name"`
Provider string `mapstructure:"provider" json:"provider" yaml:"provider"`
Enabled bool `mapstructure:"enabled" json:"enabled" yaml:"enabled"`
DefaultTTL time.Duration `mapstructure:"default_ttl" json:"default_ttl" yaml:"default_ttl"`
}
Config holds provider-agnostic cache configuration.
func (*Config) ApplyDefaults ¶
func (c *Config) ApplyDefaults()
ApplyDefaults fills zero-valued fields.
type ConfigTypeError ¶
ConfigTypeError reports an adapter-specific config type mismatch.
func (*ConfigTypeError) Error ¶
func (e *ConfigTypeError) Error() string
type FactoryRegistry ¶
type FactoryRegistry struct {
// contains filtered or unexported fields
}
FactoryRegistry stores cache backend factories by provider name.
func NewFactoryRegistry ¶
func NewFactoryRegistry() *FactoryRegistry
NewFactoryRegistry creates an isolated cache factory registry.
type FileConfig ¶
type FileConfig struct {
// Root is the directory that holds cache entries. It is created on demand.
Root string `mapstructure:"root" json:"root" yaml:"root"`
// KeyPrefix namespaces keys so independent caches may share a Root without collisions.
KeyPrefix string `mapstructure:"key_prefix" json:"key_prefix" yaml:"key_prefix"`
// MaxEntryBytes rejects serialized entries larger than this bound. Zero selects
// the 16 MiB default.
MaxEntryBytes int64 `mapstructure:"max_entry_bytes" json:"max_entry_bytes" yaml:"max_entry_bytes"`
// DefaultTTL is applied when Set is called with ttl == 0. A resulting zero
// duration means the entry never expires.
DefaultTTL time.Duration `mapstructure:"default_ttl" json:"default_ttl" yaml:"default_ttl"`
}
FileConfig configures the filesystem cache backend.
type FileStore ¶
type FileStore struct {
// contains filtered or unexported fields
}
FileStore is a persistent cache backed by sharded files under a confined root. Entries are addressed by a content hash of their prefixed key, so keys never map onto filesystem paths directly and directory traversal is impossible. Writes are serialized and atomic (temp file + rename); reads run concurrently.
func NewFileStore ¶
func NewFileStore(cfg FileConfig) (*FileStore, error)
NewFileStore creates a filesystem cache rooted at cfg.Root, creating the root directory if necessary. Root must be non-empty.
func (*FileStore) CleanupExpired ¶
CleanupExpired scans at most maxEntries files and deletes expired entries, returning the number removed. It is application-invoked maintenance; the store performs no automatic capacity eviction.
type MemoryConfig ¶
type MemoryConfig struct {
DefaultTTL time.Duration `mapstructure:"default_ttl" json:"default_ttl" yaml:"default_ttl"`
}
MemoryConfig configures the in-memory cache backend.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is a thread-safe in-memory cache with TTL expiration.
func NewMemoryStore ¶
func NewMemoryStore(cfg MemoryConfig) *MemoryStore
NewMemoryStore creates an in-memory cache.
func (*MemoryStore) Delete ¶
func (s *MemoryStore) Delete(_ context.Context, key string) error
Delete removes a key.
type Store ¶
type Store interface {
Get(ctx context.Context, key string) ([]byte, bool, error)
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Delete(ctx context.Context, key string) error
Exists(ctx context.Context, key string) (bool, error)
}
Store is the backend-neutral cache contract.
type TypedStore ¶
type TypedStore[C any] struct { // contains filtered or unexported fields }
TypedStore provides JSON-serialized typed cache operations.
func NewTypedStore ¶
func NewTypedStore[C any](store Store, keyPrefix string) *TypedStore[C]
NewTypedStore creates a typed cache store.
func (*TypedStore[C]) Delete ¶
func (s *TypedStore[C]) Delete(ctx context.Context, key string) error
Delete removes key.