memorystore

package
v0.99.1 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultKVSweepInterval = time.Minute
	DefaultKVMaxEntries    = 100_000
)

Variables

View Source
var ErrKVFull = errors.New("memorystore: kv entry cap reached")

ErrKVFull is returned by Set when the store holds MaxEntries live keys and sweeping expired ones frees nothing: the bound on heap growth is a hard refusal, never a silent eviction of someone else's live token.

Functions

This section is empty.

Types

type KV

type KV struct {
	// contains filtered or unexported fields
}

KV is an in-memory key-value store with TTL support for single-process deployments. Expired entries are reclaimed by a background sweep (#305) as well as on access, and the live entry count is capped.

func NewKV

func NewKV(opts ...KVOption) *KV

func (*KV) Close added in v0.98.0

func (k *KV) Close()

Close stops the background sweep. Idempotent.

func (*KV) Consume

func (k *KV) Consume(ctx context.Context, key string) ([]byte, bool, error)

Consume atomically returns and deletes a key under a single lock hold, so a value is delivered to AT MOST ONE caller even under concurrent reads — the in-memory analogue of Redis GETDEL. Required for single-use credentials whose KEY is the secret (passkey challenge, password-reset token); a Get+Del pair would let two concurrent requests both observe the value before either deletes. Missing or expired key => (nil, false, nil).

func (*KV) Del

func (k *KV) Del(ctx context.Context, key string) error

func (*KV) Get

func (k *KV) Get(ctx context.Context, key string) ([]byte, bool, error)

func (*KV) Incr added in v0.98.0

func (k *KV) Incr(ctx context.Context, key string, ttl time.Duration) (int64, error)

Incr is the in-memory analogue of the Redis INCR+PEXPIRE script: read, increment and write happen under one lock hold, so concurrent callers see distinct consecutive values. The TTL is set only when the key is created, and creation is bounded by the entry cap exactly like Set.

func (*KV) Len added in v0.98.0

func (k *KV) Len() int

Len reports the number of stored entries, expired ones included until swept.

func (*KV) Set

func (k *KV) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

type KVOption added in v0.98.0

type KVOption func(*KV)

func WithKVClock added in v0.98.0

func WithKVClock(now func() time.Time) KVOption

WithKVClock replaces the TTL clock (tests advance it instead of sleeping).

func WithMaxEntries added in v0.98.0

func WithMaxEntries(n int) KVOption

WithMaxEntries caps the number of live entries; Set refuses beyond it.

func WithSweepInterval added in v0.98.0

func WithSweepInterval(d time.Duration) KVOption

WithSweepInterval sets how often expired entries are reclaimed without being read. Non-positive disables the sweep.

type SIWSCache

type SIWSCache struct {
	// contains filtered or unexported fields
}

SIWSCache stores pending SIWS challenges in memory. This is only suitable for single-node deployments or local development.

func NewSIWSCache

func NewSIWSCache(ttl time.Duration, opts ...SIWSCacheOption) *SIWSCache

NewSIWSCache creates a new in-memory SIWS challenge cache.

func (*SIWSCache) Close

func (c *SIWSCache) Close() error

Close stops the background cleanup goroutine. Should be called when the cache is no longer needed.

func (*SIWSCache) Consume

func (c *SIWSCache) Consume(ctx context.Context, nonce string) (siws.ChallengeData, bool, error)

Consume atomically retrieves and deletes a challenge (single-use). The write lock makes get+delete a single-winner operation, so two concurrent callers presenting the same nonce can't both verify — closing the SIWS replay window.

func (*SIWSCache) Del

func (c *SIWSCache) Del(ctx context.Context, nonce string) error

Del removes a challenge from memory.

func (*SIWSCache) Get

func (c *SIWSCache) Get(ctx context.Context, nonce string) (siws.ChallengeData, bool, error)

Get retrieves a challenge from memory.

func (*SIWSCache) Put

func (c *SIWSCache) Put(ctx context.Context, nonce string, data siws.ChallengeData) error

Put stores a challenge in memory.

type SIWSCacheOption added in v0.98.0

type SIWSCacheOption func(*SIWSCache)

func WithSIWSCacheClock added in v0.98.0

func WithSIWSCacheClock(now func() time.Time) SIWSCacheOption

WithSIWSCacheClock replaces the TTL clock (tests advance it instead of sleeping).

type StateCache

type StateCache struct {
	// contains filtered or unexported fields
}

StateCache is an in-memory implementation of oidckit.StateCache with TTL.

func NewStateCache

func NewStateCache(ttl time.Duration) *StateCache

NewStateCache creates a new in-memory state cache with the given TTL. If ttl <= 0, a default of 10 minutes is used. Starts a background goroutine to clean up expired entries every minute.

func (*StateCache) Close

func (s *StateCache) Close() error

Close stops the background cleanup goroutine. Should be called when the cache is no longer needed.

func (*StateCache) Consume

func (s *StateCache) Consume(ctx context.Context, state string) (oidckit.StateData, bool, error)

Consume atomically returns and deletes the state in one locked step, closing the replay/TOCTOU window a separate Get+Del leaves open. ok=false if the state is absent, expired, or already consumed.

func (*StateCache) Del

func (s *StateCache) Del(ctx context.Context, state string) error

func (*StateCache) Get

func (s *StateCache) Get(ctx context.Context, state string) (oidckit.StateData, bool, error)

func (*StateCache) Put

func (s *StateCache) Put(ctx context.Context, state string, v oidckit.StateData) error

Jump to

Keyboard shortcuts

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