Documentation
¶
Overview ¶
Package cache provides a small cache contract with in-memory and Redis drivers behind one small interface.
Index ¶
- func Remember[T any](ctx context.Context, store Store, key string, ttl time.Duration, ...) (T, error)
- type Memory
- func (m *Memory) Close() error
- func (m *Memory) Forget(_ context.Context, key string) error
- func (m *Memory) Get(_ context.Context, key string) (string, bool, error)
- func (m *Memory) Increment(_ context.Context, key string, by int64) (int64, error)
- func (m *Memory) Set(_ context.Context, key, value string, ttl time.Duration) error
- type MemoryOptions
- type Redis
- func (r *Redis) Close() error
- func (r *Redis) Forget(ctx context.Context, key string) error
- func (r *Redis) Get(ctx context.Context, key string) (string, bool, error)
- func (r *Redis) Increment(ctx context.Context, key string, by int64) (int64, error)
- func (r *Redis) Set(ctx context.Context, key, value string, ttl time.Duration) error
- type RedisOptions
- type Store
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Remember ¶
func Remember[T any](ctx context.Context, store Store, key string, ttl time.Duration, compute func(context.Context) (T, error)) (T, error)
Remember returns the cached JSON-decoded value for key, computing and storing it on a miss. A stored value that no longer decodes into T is treated as a miss and recomputed, so shape changes self-heal.
Types ¶
type Memory ¶
type Memory struct {
// contains filtered or unexported fields
}
Memory is an in-process Store suitable for development and single-instance deployments. It is unbounded; use the Redis driver when eviction or shared state is needed.
func NewMemory ¶
func NewMemory(options MemoryOptions) *Memory
NewMemory performs this package operation.
type MemoryOptions ¶
type MemoryOptions struct {
// CleanupInterval between janitor sweeps of expired entries; defaults to
// one minute. A negative interval disables the janitor.
CleanupInterval time.Duration
}
MemoryOptions defines an implementation type used by this package.
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis is a Store backed by a shared Redis client.
func NewRedis ¶
func NewRedis(options RedisOptions) (*Redis, error)
NewRedis performs this package operation.
type RedisOptions ¶
type RedisOptions struct {
// Client is required. The caller owns it and closes it; the runtime
// shares one client between the cache and other Redis consumers.
Client *redis.Client
// Prefix is prepended to every key, e.g. "myapp:".
Prefix string
}
RedisOptions defines an implementation type used by this package.
type Store ¶
type Store interface {
// Get returns the value for key; ok reports whether the key existed.
Get(ctx context.Context, key string) (value string, ok bool, err error)
// Set stores value under key. A ttl of zero or less stores it without
// expiry.
Set(ctx context.Context, key, value string, ttl time.Duration) error
// Forget removes key; deleting a missing key is not an error.
Forget(ctx context.Context, key string) error
// Increment adds by to the integer stored under key, starting from zero
// when the key is missing, and returns the new value. A non-integer value
// is an error.
Increment(ctx context.Context, key string, by int64) (int64, error)
// Close define an operation required by this interface.
Close() error
}
Store is the cache contract shared by every driver.