Documentation
¶
Index ¶
- Constants
- Variables
- func ErrDriverError(driver string, err error) error
- func ErrInvalidConfig(format string, args ...interface{}) error
- func MustResolve(app foundation.Application) cache.Cache
- func MustResolveStore(app foundation.Application, name string) cache.Store
- func RegisterDriver(name string, factory DriverFactory)
- func Resolve(app foundation.Application) (cache.Cache, error)
- func ResolveStore(app foundation.Application, name string) (cache.Store, error)
- type CacheServiceProvider
- func (p *CacheServiceProvider) Boot(app foundation.Application) error
- func (p *CacheServiceProvider) Dependencies() []string
- func (p *CacheServiceProvider) Name() string
- func (p *CacheServiceProvider) Register(app foundation.Application) error
- func (p *CacheServiceProvider) Shutdown(app foundation.Application) error
- func (p *CacheServiceProvider) Version() string
- type Config
- type DriverFactory
- type Injectable
- type Item
- type Manager
- func (m *Manager) Close() error
- func (m *Manager) Decrement(ctx context.Context, key string, value int64) (int64, error)
- func (m *Manager) DefaultStore() cache.Store
- func (m *Manager) Flush(ctx context.Context) error
- func (m *Manager) Forever(ctx context.Context, key string, value interface{}) error
- func (m *Manager) Forget(ctx context.Context, key string) error
- func (m *Manager) ForgetMultiple(ctx context.Context, keys []string) error
- func (m *Manager) Get(ctx context.Context, key string) (interface{}, error)
- func (m *Manager) GetAs(ctx context.Context, key string, dest interface{}) error
- func (m *Manager) GetBool(ctx context.Context, key string) (bool, error)
- func (m *Manager) GetFloat64(ctx context.Context, key string) (float64, error)
- func (m *Manager) GetInt(ctx context.Context, key string) (int, error)
- func (m *Manager) GetInt64(ctx context.Context, key string) (int64, error)
- func (m *Manager) GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error)
- func (m *Manager) GetPrefix() string
- func (m *Manager) GetString(ctx context.Context, key string) (string, error)
- func (m *Manager) Has(ctx context.Context, key string) (bool, error)
- func (m *Manager) Increment(ctx context.Context, key string, value int64) (int64, error)
- func (m *Manager) Missing(ctx context.Context, key string) (bool, error)
- func (m *Manager) Pull(ctx context.Context, key string) (interface{}, error)
- func (m *Manager) Put(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func (m *Manager) PutMultiple(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
- func (m *Manager) RegisterDriver(name string, factory DriverFactory)
- func (m *Manager) RegisterMetrics() error
- func (m *Manager) Remember(ctx context.Context, key string, ttl time.Duration, ...) (interface{}, error)
- func (m *Manager) RememberForever(ctx context.Context, key string, callback func() (interface{}, error)) (interface{}, error)
- func (m *Manager) SetPrefix(prefix string)
- func (m *Manager) Stats() cache.Stats
- func (m *Manager) Stop(ctx context.Context) error
- func (m *Manager) Store(name string) (cache.Store, error)
- func (m *Manager) Tags(tags ...string) cache.TaggedStore
- type StoreConfig
Constants ¶
const ( Binding = "cache" Version = "1.0.0" )
Variables ¶
var ( // ErrKeyNotFound is returned when a cache key is not found. ErrKeyNotFound = fmt.Errorf("cache: key not found") // ErrInvalidValue is returned when a cache value is invalid. ErrInvalidValue = fmt.Errorf("cache: invalid value") // ErrDriverNotFound is returned when a cache driver is not found. ErrDriverNotFound = fmt.Errorf("cache: driver not found") // ErrStoreNotFound is returned when a cache store is not found. ErrStoreNotFound = fmt.Errorf("cache: store not found") )
Error types for cache operations.
Functions ¶
func ErrDriverError ¶
ErrDriverError returns a driver error with a formatted message.
func ErrInvalidConfig ¶
ErrInvalidConfig returns a configuration error with a formatted message.
func MustResolve ¶
func MustResolve(app foundation.Application) cache.Cache
MustResolve resolves the cache manager or panics.
func MustResolveStore ¶
func MustResolveStore(app foundation.Application, name string) cache.Store
MustResolveStore resolves a named store or panics.
func RegisterDriver ¶
func RegisterDriver(name string, factory DriverFactory)
RegisterDriver registers a driver factory globally.
func Resolve ¶
func Resolve(app foundation.Application) (cache.Cache, error)
Resolve resolves the main cache manager from the application container.
func ResolveStore ¶
func ResolveStore(app foundation.Application, name string) (cache.Store, error)
ResolveStore resolves a named cache store from the container.
Types ¶
type CacheServiceProvider ¶
type CacheServiceProvider struct {
// Config holds cache configuration
Config Config `config:"cache"`
// DriverFactories maps driver names to their factory functions
DriverFactories map[string]DriverFactory
}
CacheServiceProvider implements the PluginProvider interface.
func NewCacheServiceProvider ¶
func NewCacheServiceProvider(driverFactories map[string]DriverFactory) *CacheServiceProvider
NewCacheServiceProvider creates a new cache service provider.
func (*CacheServiceProvider) Boot ¶
func (p *CacheServiceProvider) Boot(app foundation.Application) error
Boot boots the cache service provider.
func (*CacheServiceProvider) Dependencies ¶
func (p *CacheServiceProvider) Dependencies() []string
Dependencies returns the list of dependencies.
func (*CacheServiceProvider) Name ¶
func (p *CacheServiceProvider) Name() string
Name returns the name of the plugin.
func (*CacheServiceProvider) Register ¶
func (p *CacheServiceProvider) Register(app foundation.Application) error
Register registers the cache service provider.
func (*CacheServiceProvider) Shutdown ¶
func (p *CacheServiceProvider) Shutdown(app foundation.Application) error
Shutdown gracefully closes cache connections.
func (*CacheServiceProvider) Version ¶
func (p *CacheServiceProvider) Version() string
Version returns the version of the plugin.
type Config ¶
type Config struct {
// DefaultStore is the name of the default cache store to use.
DefaultStore string `mapstructure:"default_store"`
// Prefix is the global cache key prefix.
// This is prepended to all cache keys.
Prefix string `mapstructure:"prefix"`
// Stores contains the configuration for each cache store.
Stores map[string]StoreConfig `mapstructure:"stores"`
}
Config represents the cache configuration.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a default cache configuration.
func (Config) WithDefaultStore ¶
WithDefaultStore sets the default store name.
func (Config) WithPrefix ¶
WithPrefix sets the global cache key prefix.
type DriverFactory ¶
type DriverFactory func(config StoreConfig) (cache.Driver, error)
DriverFactory is a function that creates a cache driver.
type Injectable ¶
type Injectable struct {
// contains filtered or unexported fields
}
Injectable provides a convenient way to inject cache dependencies.
func NewInjectable ¶
func NewInjectable(app foundation.Application) *Injectable
NewInjectable creates a new Injectable instance.
func (*Injectable) Cache ¶
func (i *Injectable) Cache() cache.Cache
Cache returns the main cache manager.
type Item ¶
type Item struct {
// Key is the cache key.
Key string
// Value is the cached value.
Value interface{}
// ExpiresAt is the expiration time.
// Zero value means the item never expires.
ExpiresAt time.Time
// Tags are the tags associated with this item.
Tags []string
}
Item represents a cache item with metadata.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages multiple cache stores and provides a unified interface.
func NewManager ¶
func (*Manager) DefaultStore ¶
DefaultStore returns the default cache store.
func (*Manager) ForgetMultiple ¶
ForgetMultiple removes multiple values from the default cache store.
func (*Manager) GetAs ¶
GetAs retrieves a value and unmarshals it into the provided destination pointer. This provides type-safe retrieval with automatic deserialization.
func (*Manager) GetFloat64 ¶
GetFloat64 retrieves a float64 value from the cache.
func (*Manager) GetMultiple ¶
GetMultiple retrieves multiple values from the default cache store.
func (*Manager) PutMultiple ¶
func (m *Manager) PutMultiple(ctx context.Context, items map[string]interface{}, ttl time.Duration) error
PutMultiple stores multiple values in the default cache store.
func (*Manager) RegisterDriver ¶
func (m *Manager) RegisterDriver(name string, factory DriverFactory)
RegisterDriver registers a driver factory for the given driver name.
func (*Manager) RegisterMetrics ¶
RegisterMetrics registers cache metrics with OpenTelemetry.
func (*Manager) Remember ¶
func (m *Manager) Remember(ctx context.Context, key string, ttl time.Duration, callback func() (interface{}, error)) (interface{}, error)
Remember retrieves a value from the cache or executes the callback and stores the result. This implements the cache-aside pattern.
func (*Manager) RememberForever ¶
func (m *Manager) RememberForever(ctx context.Context, key string, callback func() (interface{}, error)) (interface{}, error)
RememberForever retrieves a value from the cache or executes the callback and stores the result forever.
func (*Manager) Stop ¶
Stop stops the cache manager gracefully. This implements the Stoppable interface.
type StoreConfig ¶
type StoreConfig struct {
// Driver is the cache driver name (e.g., "redis", "memory").
Driver string `mapstructure:"driver"`
// Connection is the connection name to use (for drivers that support multiple connections).
Connection string `mapstructure:"connection"`
// Prefix is the store-specific cache key prefix.
// This overrides the global prefix for this store.
Prefix string `mapstructure:"prefix"`
// Options contains driver-specific configuration options.
Options map[string]interface{} `mapstructure:"options"`
}
StoreConfig represents the configuration for a single cache store.
func (StoreConfig) Decode ¶
func (c StoreConfig) Decode(target interface{}) error
Decode decodes the store options into the target struct.