memory

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package memory provides an in-process cache.Cache implementation.

Properties:

  • Map-backed, sync.RWMutex-guarded; safe for concurrent use.
  • TTL is honoured on read AND swept lazily by an optional janitor goroutine (interval set via Options.SweepEvery; 0 disables).
  • Optional MaxEntries cap with a tiny FIFO eviction order (oldest SetTime first). Not LRU — LRU adds an extra synchronisation point per Get; if you need it, wrap an LRU pkg or vendor your own. FIFO is a reasonable default for typical web caches.
  • drops.Hook fires after every operation with kind = "cache.get", "cache.set", "cache.del", "cache.exists", "cache.ttl", "cache.ping".

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 the in-memory implementation of cache.Cache.

func New

func New(opts ...Options) *Cache

New returns a memory-backed cache. The janitor (if enabled) is started immediately; Close stops it.

func (*Cache) Close

func (c *Cache) Close() error

Close stops the janitor goroutine and rejects subsequent calls.

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, keys ...string) (n int, err error)

Delete removes the listed keys, returning the count that existed.

func (*Cache) Exists

func (c *Cache) Exists(ctx context.Context, key string) (_ bool, err error)

Exists reports presence (non-expired) for a key.

func (*Cache) Get

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

Get returns the value or cache.ErrNotFound.

func (*Cache) GetMulti

func (c *Cache) GetMulti(ctx context.Context, keys ...string) (_ map[string][]byte, err error)

GetMulti returns every key found.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the current entry count (for tests / metrics).

func (*Cache) Ping

func (c *Cache) Ping(ctx context.Context) (err error)

Ping is a no-op for the in-memory cache (always healthy unless closed).

func (*Cache) Set

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

Set stores value under key.

func (*Cache) SetMulti

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

SetMulti stores every item with the same TTL.

func (*Cache) TTL

func (c *Cache) TTL(ctx context.Context, key string) (_ time.Duration, err error)

TTL returns the remaining lifetime, or -1 for "no expiry", or 0 (with ErrNotFound) for absent keys.

type Options

type Options struct {
	// MaxEntries caps the live entry count. When inserting beyond it,
	// the oldest-inserted entry is evicted. 0 means unbounded.
	MaxEntries int

	// SweepEvery is the janitor interval that removes expired entries.
	// 0 disables the janitor; expired entries are still skipped on
	// read but the map will grow until something overwrites them.
	SweepEvery time.Duration

	// Hook fires after every operation, suitable for logging / metrics.
	Hook drops.Hook

	// Clock allows tests to inject a virtual clock; defaults to time.Now.
	Clock func() time.Time
}

Options tunes the in-memory cache.

Jump to

Keyboard shortcuts

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