memory

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

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

New captures options and returns a DI-injectable constructor.

Types

type Config

type Config struct {
	env.WithGasEnv

	Cache Settings
}

Config holds in-memory cache settings.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with sensible defaults.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks the Config for correctness.

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() 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.

func (*Service) Exists

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

Exists checks whether a key exists and has not expired.

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.

func (*Service) Init

func (s *Service) Init() error

Init validates the configuration and starts the background cleanup goroutine.

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, the oldest expired entry is evicted first; if none are expired, the set is rejected with an error.

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