memory

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package memory provides a race-safe in-process cache store.

Index

Constants

This section is empty.

Variables

View Source
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.

func (*Locker[K]) Lock

func (l *Locker[K]) Lock(ctx context.Context, namespace string, key K, ttl time.Duration) (cache.Lease, error)

Lock acquires a lease on key in namespace for ttl. It returns cache.ErrLockNotAcquired while another lease is live and cache.ErrInvalidTTL for a non-positive ttl.

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

func (s *Store[K, V]) Close() error

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

func (s *Store[K, V]) Invalidate(ctx context.Context, namespace string, key K) error

Invalidate removes key from namespace. Removing a missing key is not an error.

func (*Store[K, V]) InvalidateAll

func (s *Store[K, V]) InvalidateAll(ctx context.Context, namespace string) error

InvalidateAll removes every entry in namespace.

func (*Store[K, V]) InvalidateMany

func (s *Store[K, V]) InvalidateMany(ctx context.Context, namespace string, keys []K) error

InvalidateMany removes keys under one lock acquisition.

func (*Store[K, V]) Len

func (s *Store[K, V]) Len() int

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

func (s *Store[K, V]) Set(ctx context.Context, namespace string, key K, entry cache.Entry[V]) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL