cachememory

package
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package cachememory provides a bounded, concurrency-safe LRU cache adapter. Entry/byte accounting is not a heap cap. Expiration is lazy at StaleAt, and Stats does not sweep. Close releases retained entries; no janitor is started. Clock callbacks run under the mutex; observers run synchronously after it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

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

Backend is a bounded concurrency-safe LRU backend.

func New

func New(config Config) (*Backend, error)

New validates config and constructs an empty memory backend.

func (*Backend) Close

func (b *Backend) Close() error

Close releases retained entries and rejects subsequent operations.

func (*Backend) Delete

func (b *Backend) Delete(ctx context.Context, key string) (bool, error)

Delete removes key and reports whether it was present.

func (*Backend) Get

func (b *Backend) Get(ctx context.Context, key string) (cache.Record, bool, error)

Get returns a cloned live record or an explicit miss.

func (*Backend) Set

func (b *Backend) Set(
	ctx context.Context,
	key string,
	record cache.Record,
	condition cache.Condition,
) (bool, error)

Set atomically applies condition and stores a cloned record. A structurally valid past hard deadline is admitted and removed lazily on access. Size means key+payload bytes, not heap overhead. Eviction may remove multiple entries.

func (*Backend) Stats

func (b *Backend) Stats() Stats

Stats returns a synchronized snapshot without sweeping expired entries. Removal counters survive Close, which clears retained entries/bytes.

type Config

type Config struct {
	// MaxEntries bounds retained entries, not all allocations or initial map overhead.
	MaxEntries int
	// MaxBytes bounds retained key+payload bytes, excluding map/list/runtime overhead.
	MaxBytes int
	// Clock is retained and called under the backend mutex; it must not re-enter.
	Clock cache.Clock
	// Observer optionally receives removal events after unlocking; it may block the call.
	Observer cache.Observer
}

Config defines hard entry and retained-byte limits for a Backend.

type Stats

type Stats struct {
	// Entries counts retained entries, including expired entries not yet touched.
	Entries int
	// Bytes counts retained key+payload bytes, not total process memory.
	Bytes int
	// Evictions counts capacity removals since construction, not explicit deletes.
	Evictions uint64
	// Expirations counts lazy hard-deadline removals since construction.
	Expirations uint64
}

Stats is a snapshot of retained state and cumulative removals.

Jump to

Keyboard shortcuts

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