memory

package
v0.5.6 Latest Latest
Warning

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

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

Documentation

Overview

Package memory is the in-process gas.CacheProvider backend. Its Service keeps entries in a map guarded by a mutex, so the cache is local to one process and is lost when it stops. It also implements gas.Service.

Wiring

New returns a DI constructor that takes a gas.ConfigProvider and a gas.Logger and returns *Service; register it under gas.CacheProvider. The constructor does not return an error, so it is not yet on the shape in CONVENTIONS.md (KI-004 in KNOWN_ISSUES.md). WithConfig supplies the configuration directly and skips binding it from the ConfigProvider.

Configuration

Without WithConfig, the service binds a Config from the ConfigProvider over DefaultConfig when it starts. The cache settings are MaxEntries (default 0, unlimited), CleanupInterval (default one minute; 0 or less disables background cleanup), and DefaultTTL (default 0, no expiry). Validate accepts any values.

Lifecycle

Starting the service binds the configuration, creates an empty map, and, when CleanupInterval is positive, starts a goroutine that removes expired entries on that interval until the service stops. Stopping it cancels that goroutine, drops every entry, and makes every operation return cache.ErrClosed. Starting it again begins with an empty cache. Before the service has started, every operation also returns cache.ErrClosed. Each operation checks this under the mutex, so a stop that races an operation makes it return cache.ErrClosed rather than touch the dropped map.

Behavior

A Set with a TTL of 0 uses DefaultTTL; a positive TTL, from either source, sets an expiry, and a negative one stores the entry without expiry. Expired entries are invisible to Get and Exists, and Get deletes the expired entry it finds. When MaxEntries is reached, a Set of a new key removes one expired entry, chosen in map iteration order rather than by age, and fails with an error if there is none; overwriting an existing key always succeeds. Values are stored and returned as the same slice, not copies, so a caller that modifies a slice it passed to Set or got from Get changes the cached value.

SPDX-License-Identifier: MIT

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) func(gas.ConfigProvider, gas.Logger) (*Service, error)

New captures options and returns a DI-injectable constructor.

Types

type Config

type Config struct {
	gas.WithGasEnv

	Cache Settings
}

Config holds in-memory cache settings.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with sensible defaults.

type Option

type Option func(*Service)

Option configures a Service.

func WithConfig

func WithConfig(cfg *Config) Option

WithConfig sets a custom configuration.

type Service

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

Service is an in-memory cache implementing gas.Service and gas.CacheProvider. It uses a simple map with optional background cleanup of expired entries.

func (*Service) Close

func (s *Service) Close(context.Context) error

Close stops the background cleanup goroutine and clears the cache.

func (*Service) Delete

func (s *Service) Delete(_ context.Context, key string) error

Delete removes a key from the cache. It returns cache.ErrClosed if the service was never started or has been closed.

func (*Service) Exists

func (s *Service) Exists(_ context.Context, key string) (bool, error)

Exists checks whether a key exists and has not expired. It returns cache.ErrClosed if the service was never started or has been closed.

func (*Service) Get

func (s *Service) Get(_ context.Context, key string) ([]byte, error)

Get returns the value for the given key, or cache.ErrKeyNotFound if the key does not exist or has expired. It returns cache.ErrClosed if the service was never started or has been closed.

func (*Service) Init

func (s *Service) Init(context.Context) error

Init binds the configuration and starts the background cleanup goroutine. The goroutine does not inherit ctx; it runs until Close.

func (*Service) Name

func (s *Service) Name() string

Name returns the service identifier.

func (*Service) Set

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

Set stores a value with the given TTL. If ttl is 0, the configured DefaultTTL is used. If DefaultTTL is also 0, the entry never expires. When MaxEntries is reached and a new key is inserted, one expired entry, chosen in map iteration order rather than by age, is evicted first; if none are expired, the set is rejected with an error. It returns cache.ErrClosed if the service was never started or has been closed.

type Settings

type Settings struct {
	// MaxEntries is the maximum number of entries in the cache.
	// 0 means unlimited.
	MaxEntries int

	// CleanupInterval is how often expired entries are evicted.
	// 0 disables background cleanup.
	CleanupInterval time.Duration

	// DefaultTTL is the default time-to-live for entries when Set is
	// called with ttl == 0. A value of 0 means entries never expire.
	DefaultTTL time.Duration
}

Settings represents the configuration for the in-memory cache.

Jump to

Keyboard shortcuts

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