memstore

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package memstore provides a generic, thread-safe, keyed in-memory store with optional buffered write-behind persistence. The stored content type, its validation, and its persisted encoding (the "flavor") live with the module that manages the data — this package only owns concurrency, write policy, pruning, rehydration, and flush batching.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Persistence

type Persistence[K comparable, V any] interface {
	Load() (map[K]V, error)
	PersistBatch(upserts map[K]V, deletes []K) error
}

Persistence adapts a store to a backing table. Load is called once when the adapter is attached; PersistBatch is called from the store's flush loop with everything that changed since the last flush (upserts + deletions) and must apply the batch in a single transaction. Best-effort: on error the store logs and retries the same batch on the next flush.

type Store

type Store[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Store is a generic, thread-safe, keyed in-memory store with a configurable write policy (last-write-wins or keep-existing) and optional buffered write-behind persistence. Writers never touch the persistence backend on their hot path; changes are tracked in dirty/deleted sets drained by the store's own flush loop.

func New

func New[K comparable, V any]() *Store[K, V]

New creates a store with the last-write-wins policy: Put always replaces.

func NewKeepExisting

func NewKeepExisting[K comparable, V any]() *Store[K, V]

NewKeepExisting creates a store with the first-write-wins policy: Put is rejected when the key is already present.

func (*Store[K, V]) Clear

func (s *Store[K, V]) Clear()

Clear removes all entries, marking every deletion for the next flush.

func (*Store[K, V]) Delete

func (s *Store[K, V]) Delete(key K)

Delete removes key from the store and marks the deletion for the next flush. No-op when the key is absent.

func (*Store[K, V]) Entries

func (s *Store[K, V]) Entries() map[K]V

Entries returns a copy of the stored entries; mutating the returned map does not affect the store.

func (*Store[K, V]) Flush

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

Flush synchronously persists all pending changes in one batch. On failure the batch is re-marked and retried on the next flush. No-op when no persistence is attached or nothing is pending.

func (*Store[K, V]) Get

func (s *Store[K, V]) Get(key K) (V, bool)

Get returns the value stored under key.

func (*Store[K, V]) Has

func (s *Store[K, V]) Has(key K) bool

Has reports whether key is present.

func (*Store[K, V]) Len

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

Len returns the number of stored entries.

func (*Store[K, V]) Prune

func (s *Store[K, V]) Prune(drop func(key K) bool) int

Prune removes every key for which drop returns true, marking the deletions for the next flush, and returns the number of removed entries. The drop predicate is called with the store lock held and must not call back into the store.

func (*Store[K, V]) Put

func (s *Store[K, V]) Put(key K, value V) bool

Put stores value under key. With the keep-existing policy it returns false and leaves the store untouched when the key is already present; otherwise it returns true.

func (*Store[K, V]) SetPersistence

func (s *Store[K, V]) SetPersistence(ctx context.Context, p Persistence[K, V], log logrus.FieldLogger)

SetPersistence attaches the persistence adapter, rehydrates the store via Load() (loaded entries never overwrite in-memory ones written before attach), and starts the flush loop. Entries written before attach are marked dirty so they reach the backing table on the first flush. Attach at most once; a second call is ignored with a warning.

func (*Store[K, V]) Stop

func (s *Store[K, V]) Stop()

Stop terminates the flush loop and synchronously flushes all pending changes. Must be called before the backing database closes. No-op when no persistence is attached; idempotent.

func (*Store[K, V]) Values

func (s *Store[K, V]) Values() []V

Values returns a snapshot slice of all stored values.

Jump to

Keyboard shortcuts

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