memory

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package memory provides a process-local byte cache with synchronous writes.

Index

Constants

View Source
const DefaultMaxBytes int64 = 64 << 20

DefaultMaxBytes bounds retained key/value bytes, not total process memory.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Capacity      int
	MaxEntryBytes int
	MaxBytes      int64
}

Config limits retained entries and key/value bytes. MaxBytes defaults to DefaultMaxBytes when zero. Capacity and MaxEntryBytes must be positive. Map metadata, allocator overhead and caller-owned Get results are additional.

func (Config) Validate

func (c Config) Validate() error

Validate checks limits without allocating a cache. A zero MaxBytes uses the default.

type Store

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

Store supports concurrent use. Its zero value is not usable; use New. It rejects writes with ErrFull instead of evicting live entries. Constructors start no goroutines. Expiration is lazy; PruneExpired allows explicit cleanup.

func New

func New(capacity, maxEntryBytes int) (*Store, error)

New creates a cache with a 64 MiB key/value budget. Use NewWithConfig to override that budget. Each entry includes its key in maxEntryBytes.

func NewWithConfig

func NewWithConfig(cfg Config) (*Store, error)

NewWithConfig constructs an empty cache without preallocating its byte budget.

func (*Store) Clear

func (s *Store) Clear(ctx context.Context) error

Clear releases retained entries and keeps the store usable. It is atomic relative to memory-store writes; a later write may immediately refill it.

func (*Store) Close

func (s *Store) Close(context.Context) error

Close releases all retained storage and is idempotent. It does not cancel in-flight reads; stop users before closing if their completion must be joined.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, key string) error

func (*Store) Exists

func (s *Store) Exists(ctx context.Context, key string) (bool, error)

func (*Store) Get

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

func (*Store) GetInto

func (s *Store) GetInto(ctx context.Context, key string, dst []byte) ([]byte, bool, error)

GetInto appends a hit to dst, reusing its capacity when possible. The returned slice belongs to the caller; the cache never retains dst. On a miss or error, dst is returned unchanged. Pass dst[:0] to reuse a buffer between reads. This optional memory-specific API avoids allocating a result on every hit.

func (*Store) GetMulti

func (s *Store) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)

func (*Store) PruneExpired

func (s *Store) PruneExpired(ctx context.Context, limit int) (int, error)

PruneExpired removes at most limit expired entries (1–10000). It may inspect all entries under the write lock; schedule modest batches outside hot paths. Cancellation can return both a positive removal count and an error. No live entry is evicted. The caller owns any scheduling and shutdown of maintenance.

func (*Store) Remember

func (s *Store) Remember(ctx context.Context, key string, ttl time.Duration, load func(context.Context) (any, error)) (any, error)

Remember returns a cached JSON value or loads it on a miss. See cache.Remember for validation, error, expiry and concurrent-loading semantics.

func (*Store) Set

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

func (*Store) SetMulti

func (s *Store) SetMulti(ctx context.Context, items map[string][]byte, ttl time.Duration) error

func (*Store) Usage

func (s *Store) Usage() Usage

Usage reports retained logical storage without scanning entries.

type Usage

type Usage struct {
	Entries  int
	Bytes    int64
	Capacity int
	MaxBytes int64
	Closed   bool
}

Usage is a point-in-time snapshot. Entries and Bytes include expired entries awaiting cleanup. Bytes counts keys and values only, not heap or RSS usage.

Jump to

Keyboard shortcuts

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