memory

package
v0.0.5-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package memory is an in-process cache backend. It stores values in a sync-protected map, evicts on TTL check, and is safe for concurrent use.

Memory is the default backend for tests and single-process applications that do not need cross-process cache coherence. For multi-instance deployments use hex/cache/redis or hex/cache/memcached (opt-in).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is an in-memory cache implementation.

func New

func New() *Cache

New returns an empty in-memory cache.

func WithClock

func WithClock(now func() time.Time) *Cache

WithClock returns a Cache using now for time. Intended for tests that need to advance time deterministically instead of sleeping.

func (*Cache) Clear

func (c *Cache) Clear(context.Context) error

Clear removes all entries.

func (*Cache) Delete

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

Delete removes key.

func (*Cache) Get

func (c *Cache) Get(_ context.Context, key string) ([]byte, bool, error)

Get returns the value for key, or (nil, false, nil) on miss/expiry.

func (*Cache) Has

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

Has reports whether key is present and not expired.

func (*Cache) Increment

func (c *Cache) Increment(_ context.Context, key string, delta int64) (int64, error)

Increment atomically adds delta to the value at key. Absent keys start from zero and take no expiration. Non-numeric existing values return an error.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of stored entries (including expired-but-not-yet- swept ones). Useful for tests and diagnostics.

func (*Cache) Set

func (c *Cache) Set(_ context.Context, key string, value []byte, ttl time.Duration) error

Set stores value under key. See package cache docs for TTL semantics.

Jump to

Keyboard shortcuts

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