Documentation
¶
Index ¶
- Variables
- type GenericManager
- type Manager
- func (m *Manager) Clear(ctx context.Context) error
- func (m *Manager) Delete(ctx context.Context, key string) error
- func (m *Manager) DeleteByPattern(ctx context.Context, pattern string) error
- func (m *Manager) DeleteMany(ctx context.Context, keys ...string) error
- func (m *Manager) DeleteManyByPattern(ctx context.Context, patterns ...string) error
- func (m *Manager) Get(ctx context.Context, key string) (any, error)
- func (m *Manager) GetOrSet(ctx context.Context, key string, ttl time.Duration, ...) (any, error)
- func (m *Manager) Has(ctx context.Context, key string) (bool, error)
- func (m *Manager) Register(alias string, store contract.Store) error
- func (m *Manager) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (m *Manager) SetDefault(alias string) error
- func (m *Manager) Store(alias string) *Manager
Constants ¶
This section is empty.
Variables ¶
var ( ErrCacheMiss = errors.New("cache: cache miss") ErrInternal = errors.New("cache: internal error") ErrInvalidConfig = errors.New("cache: invalid config") ErrInvalidDefaultStore = errors.New("cache: invalid default cache store") ErrInvalidStore = errors.New("cache: invalid cache store") ErrStoreAlreadyRegistered = errors.New("cache: store already registered") ErrInvalidValue = errors.New("cache: invalid value") ErrTypeMismatch = errors.New("cache: value type mismatch") )
Functions ¶
This section is empty.
Types ¶
type GenericManager ¶
type GenericManager[T any] struct { // contains filtered or unexported fields }
GenericManager provides a generic way to interact with the cache for any type T. It wraps the Manager and provides type-safe Get and GetOrSet methods.
func G ¶
func G[T any](m *Manager) *GenericManager[T]
G returns a new GenericManager for the specified type T, bound to the given Manager instance. This allows for type-safe Get and GetOrSet operations.
func (*GenericManager[T]) Get ¶
func (g *GenericManager[T]) Get(ctx context.Context, key string) (T, error)
Get retrieves a value of type T from the cache. Returns ErrTypeMismatch if the cached value cannot be converted to T.
func (*GenericManager[T]) GetOrSet ¶
func (g *GenericManager[T]) GetOrSet(ctx context.Context, key string, ttl time.Duration, defaultFn func() (T, error)) (T, error)
GetOrSet retrieves a value of type T from the cache if present; otherwise, it computes the value lazily by calling defaultFn, stores it with the given TTL, and returns it. If storing fails, it still returns the computed value along with the store error.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
func NewManager ¶
func NewManager() *Manager
func (*Manager) Clear ¶
Clear removes all keys and values from the current store. It should not return an error if the store is already empty.
func (*Manager) Delete ¶
Delete removes a single entry by key. If the key does not exist, it should return nil (no error).
func (*Manager) DeleteByPattern ¶
DeleteByPattern removes all keys matching the provided pattern. The pattern syntax depends on the underlying driver (e.g. glob for Redis, regex for memory). Drivers should document their behavior.
func (*Manager) DeleteMany ¶
DeleteMany removes multiple entries by their keys. If some keys do not exist, they are skipped without returning an error.
func (*Manager) DeleteManyByPattern ¶
DeleteManyByPattern removes entries matching any of the given patterns. Behavior follows DeleteByPattern for each pattern.
func (*Manager) Get ¶
Get retrieves a raw cached value by key. It returns ErrCacheMiss if the key is not found or has expired.
func (*Manager) GetOrSet ¶
func (m *Manager) GetOrSet(ctx context.Context, key string, ttl time.Duration, defaultFn func() (any, error)) (any, error)
GetOrSet retrieves a value from the cache if present; otherwise, it computes the value lazily by calling defaultFn, stores it with the given TTL, and returns it. If storing fails, it still returns the computed value along with the store error.
func (*Manager) Has ¶
Has reports whether the given key exists and is not expired. It should not return an error if the key simply doesn't exist.
func (*Manager) Register ¶
Register adds a new store with the given alias. The first registered store becomes the default. Returns an error if the alias is already registered.
func (*Manager) Set ¶
Set stores a value in the cache under the given key with the specified TTL. A ttl <= 0 should be treated as "no expiration" by convention, but this behavior is driver-dependent.
func (*Manager) SetDefault ¶
SetDefault sets the store with the given alias as the default store. Returns an error if the alias has not been registered.