Documentation
¶
Overview ¶
Package memory provides a race-safe in-process cache store.
Index ¶
- Variables
- type InvalidationBus
- type Locker
- type Options
- type Store
- func (s *Store[K, V]) Close() error
- 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]) Len() int
- 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 ErrInvalidOptions = errors.New("cache memory: invalid options")
ErrInvalidOptions reports Options with a non-positive MaxEntries or CleanupInterval.
Functions ¶
This section is empty.
Types ¶
type InvalidationBus ¶
type InvalidationBus[K comparable] struct { // contains filtered or unexported fields }
InvalidationBus distributes events between workers in the same process.
func NewInvalidationBus ¶
func NewInvalidationBus[K comparable]() *InvalidationBus[K]
NewInvalidationBus constructs an in-process invalidation bus.
func (*InvalidationBus[K]) Publish ¶
func (b *InvalidationBus[K]) Publish(ctx context.Context, event cache.InvalidationEvent[K]) error
Publish delivers an event to the current subscribers of its namespace.
func (*InvalidationBus[K]) Subscribe ¶
func (b *InvalidationBus[K]) Subscribe(ctx context.Context, namespace string, handler cache.InvalidationHandler[K]) error
Subscribe registers a handler until the context is canceled.
func (*InvalidationBus[K]) SubscriberCount ¶
func (b *InvalidationBus[K]) SubscriberCount(namespace string) int
SubscriberCount returns the number of handlers currently subscribed to namespace. A subscription counts from the moment Subscribe registers it until its context ends, so callers can use it to wait for listeners to be ready or to check that subscriptions are released. It is safe for concurrent use.
type Locker ¶
type Locker[K comparable] struct { // contains filtered or unexported fields }
Locker provides race-safe in-process miss coordination.
func NewLocker ¶
func NewLocker[K comparable]() *Locker[K]
NewLocker constructs an empty in-process locker.
type Options ¶
type Options struct {
// MaxEntries is the global number of live entries across all
// namespaces; the least recently used entry is evicted beyond it.
MaxEntries int
// CleanupInterval is how often the background sweeper removes expired
// and invalidated entries.
CleanupInterval time.Duration
}
Options bounds a memory Store.
type Store ¶
type Store[K comparable, V any] struct { // contains filtered or unexported fields }
Store is an in-memory cache store protected by a mutex.
func New ¶
func New[K comparable, V any]() *Store[K, V]
New constructs an empty in-memory store with bounded defaults.
func NewWithOptions ¶
func NewWithOptions[K comparable, V any](options Options) (*Store[K, V], error)
NewWithOptions constructs an empty store bounded by options and starts its expiration sweeper. It returns ErrInvalidOptions for invalid options. The store must be closed to stop the sweeper.
func (*Store[K, V]) Close ¶
Close stops the expiration sweeper and waits for it to exit. It is idempotent and always returns nil. Close does not discard stored entries.
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 looks up keys under one lock acquisition. 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 removes every entry in namespace.
func (*Store[K, V]) InvalidateMany ¶
InvalidateMany removes keys under one lock acquisition.
func (*Store[K, V]) Len ¶
Len returns the number of entries currently held across all namespaces, including expired entries the sweeper has not removed yet.
func (*Store[K, V]) Set ¶
Set stores entry under key in namespace, replacing any previous entry and marking it most recently used. The least recently used entry is evicted when the store exceeds Options.MaxEntries.
func (*Store[K, V]) SetMany ¶
func (s *Store[K, V]) SetMany(ctx context.Context, namespace string, keys []K, entries []cache.Entry[V]) error
SetMany stores entries[i] under keys[i] under one lock acquisition, so no other store operation observes a partially applied batch. Capacity eviction still applies to the resulting entries.