memory

package
v0.0.45 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package memory is the in-process ByteCache: a bounded, least-recently-used map whose entries expire at their TTL. It suits a value a service reads often, that rarely changes, and that each replica may hold its own copy of, when no shared cache is deployed; replicas never see each other's writes or deletes.

Index

Constants

View Source
const DefaultMaxEntries = 10_000

DefaultMaxEntries bounds the cache when the configured bound is not positive.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is the in-process ByteCache. It is safe for concurrent use.

func New

func New(maxEntries int, defaultTTL time.Duration, now func() time.Time) *Cache

New returns a cache holding at most maxEntries values (DefaultMaxEntries when not positive), applying defaultTTL to a zero-TTL Set and measuring expiry with now (time.Now in production).

func (*Cache) Delete

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

Delete removes key's value.

func (*Cache) Get

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

Get returns a copy of key's value and marks it recently used; an expired value is removed and misses.

func (*Cache) Set

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

Set stores a copy of value under key for ttl (the default TTL when zero), evicting the least recently used entry when the cache is full. A non-positive effective TTL stores nothing.

Jump to

Keyboard shortcuts

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