memory

package
v1.3.0 Latest Latest
Warning

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

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

Documentation

Overview

Package memory provides an in-process cache.Cache for tests and local development. It is not shared between instances: do not use it as a distributed cache in production.

Values are JSON encoded on Set and decoded on Get, mimicking the serialization boundary of network caches and preventing aliasing bugs.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidTTL = errors.New("cache/memory: ttl must be positive")

ErrInvalidTTL is returned when an entry is written without a positive TTL.

Functions

This section is empty.

Types

type Cache

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

Cache is an in-memory cache with lazy expiration.

func New

func New(opts ...Option) *Cache

New returns an empty memory cache.

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, key string) error

Delete removes key. Deleting a missing key is not an error.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key string, value any) error

Get decodes the cached value into value or returns cache.ErrMiss.

func (*Cache) Increment

func (c *Cache) Increment(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)

Increment atomically adds delta to the counter stored at key.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of live entries (test helper).

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key string, value any, ttl time.Duration) error

Set stores value under key for ttl.

type Option

type Option func(*Cache)

Option configures the memory cache.

func WithClock

func WithClock(now func() time.Time) Option

WithClock overrides the clock (useful for TTL tests).

Jump to

Keyboard shortcuts

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