Documentation
¶
Overview ¶
Package redis provides a Redis-backed cache store.
Index ¶
- Variables
- type InvalidationBus
- type InvalidationOption
- type Locker
- type LockerOption
- type Option
- type Store
- func (s *Store[K, V]) Get(ctx context.Context, namespace string, key K) (cache.Entry[V], bool, error)
- func (s *Store[K, V]) GetMany(ctx context.Context, namespace string, keys []K) ([]cache.Entry[V], []bool, error)
- func (s *Store[K, V]) Invalidate(ctx context.Context, namespace string, key K) error
- func (s *Store[K, V]) InvalidateAll(ctx context.Context, namespace string) error
- func (s *Store[K, V]) InvalidateMany(ctx context.Context, namespace string, keys []K) error
- func (s *Store[K, V]) Set(ctx context.Context, namespace string, key K, entry cache.Entry[V]) error
- func (s *Store[K, V]) SetMany(ctx context.Context, namespace string, keys []K, entries []cache.Entry[V]) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilClient = errors.New("cache redis: client must not be nil") ErrNilCodec = errors.New("cache redis: codec must not be nil") )
Functions ¶
This section is empty.
Types ¶
type InvalidationBus ¶
type InvalidationBus[K comparable] struct { // contains filtered or unexported fields }
InvalidationBus distributes invalidations through Redis Pub/Sub.
func NewInvalidationBus ¶
func NewInvalidationBus[K comparable](client goredis.UniversalClient, options ...InvalidationOption[K]) (*InvalidationBus[K], error)
NewInvalidationBus constructs a Redis Pub/Sub invalidation bus.
func (*InvalidationBus[K]) Publish ¶
func (b *InvalidationBus[K]) Publish(ctx context.Context, event cache.InvalidationEvent[K]) error
Publish sends event to the subscribers of event.Namespace.
func (*InvalidationBus[K]) Subscribe ¶
func (b *InvalidationBus[K]) Subscribe(ctx context.Context, namespace string, handler cache.InvalidationHandler[K]) error
Subscribe calls handler for every event published for namespace until ctx ends, returning ctx.Err(), or until receiving or handling an event fails, returning that error. A nil handler is rejected with cache.ErrNilInvalidationHandler.
type InvalidationOption ¶
type InvalidationOption[K comparable] func(*invalidationConfig[K]) error
InvalidationOption configures Redis invalidation serialization.
func WithInvalidationKeyCodec ¶
func WithInvalidationKeyCodec[K comparable](codec cache.Codec[K]) InvalidationOption[K]
WithInvalidationKeyCodec overrides automatic invalidation-key serialization.
type Locker ¶
type Locker[K comparable] struct { // contains filtered or unexported fields }
Locker provides renewable Redis leases for cache-fill coordination.
func NewLocker ¶
func NewLocker[K comparable](client goredis.UniversalClient, options ...LockerOption[K]) (*Locker[K], error)
NewLocker constructs a Redis locker using a caller-owned client.
type LockerOption ¶
type LockerOption[K comparable] func(*lockerConfig[K]) error
LockerOption configures Redis lock key serialization.
func WithLockerKeyCodec ¶
func WithLockerKeyCodec[K comparable](codec cache.Codec[K]) LockerOption[K]
WithLockerKeyCodec overrides automatic lock key serialization.
type Option ¶
type Option[K comparable, V any] func(*config[K, V]) error
Option configures Redis serialization.
func WithKeyCodec ¶
func WithKeyCodec[K comparable, V any](codec cache.Codec[K]) Option[K, V]
WithKeyCodec overrides automatic key serialization.
func WithValueCodec ¶
func WithValueCodec[K comparable, V any](codec cache.Codec[V]) Option[K, V]
WithValueCodec overrides automatic value serialization.
type Store ¶
type Store[K comparable, V any] struct { // contains filtered or unexported fields }
Store persists entries in Redis. The caller retains ownership of the client.
func New ¶
func New[K comparable, V any](client goredis.UniversalClient, options ...Option[K, V]) (*Store[K, V], error)
New constructs a Redis store using a caller-owned client.
func (*Store[K, V]) Get ¶
func (s *Store[K, V]) Get(ctx context.Context, namespace string, key K) (cache.Entry[V], bool, error)
Get returns the live entry stored under key in namespace and whether one is present. Expired entries and entries written before the namespace's last InvalidateAll are reported as absent.
func (*Store[K, V]) GetMany ¶
func (s *Store[K, V]) GetMany(ctx context.Context, namespace string, keys []K) ([]cache.Entry[V], []bool, error)
GetMany reads keys and the namespace generation in one pipeline round trip. Results are aligned with keys.
func (*Store[K, V]) Invalidate ¶
Invalidate removes key from namespace. Removing a missing key is not an error.
func (*Store[K, V]) InvalidateAll ¶
InvalidateAll hides every entry in namespace by advancing the namespace generation. Older records are ignored on read and left to expire or be overwritten.
func (*Store[K, V]) InvalidateMany ¶
InvalidateMany deletes keys in one pipeline round trip.
func (*Store[K, V]) Set ¶
Set stores entry under key in namespace, replacing any previous entry. An entry with a zero ExpiresAt never expires.
func (*Store[K, V]) SetMany ¶
func (s *Store[K, V]) SetMany(ctx context.Context, namespace string, keys []K, entries []cache.Entry[V]) error
SetMany writes entries in one pipeline round trip after reading the namespace generation. Already expired entries are deleted instead. The pipeline is not a transaction, so a failed call may leave part of the batch applied.