l1

package
v0.0.0-...-3472123 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package l1 provides a sharded, concurrent in-memory cache with TTL and eviction.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EntryMeta

type EntryMeta struct {
	Value        any
	TTLRemaining time.Duration
	HitCount     int
}

EntryMeta holds metadata returned by GetWithMeta.

type EvictionPolicy

type EvictionPolicy int

EvictionPolicy determines which entry is removed when MaxEntries is reached.

const (
	LRU  EvictionPolicy = iota // Least Recently Used
	LFU                        // Least Frequently Used
	FIFO                       // First In, First Out
)

type Options

type Options struct {
	TTL           time.Duration
	MaxEntries    int
	Eviction      EvictionPolicy
	SweepInterval time.Duration
	Clock         clock.Clock
	OnEvict       func(key string, value any)
}

Options configures an L1 Store.

type Stats

type Stats struct {
	Hits    int64
	Misses  int64
	Entries int64
}

Stats holds hit/miss/entry counts.

type Store

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

Store is the sharded in-memory cache.

func New

func New(opts Options) *Store

New creates a new L1 Store.

func (*Store) Close

func (s *Store) Close()

Close stops background goroutines.

func (*Store) Delete

func (s *Store) Delete(key string)

Delete removes a key from the cache.

func (*Store) Flush

func (s *Store) Flush()

Flush removes all entries from all shards.

func (*Store) FlushSchema

func (s *Store) FlushSchema(prefix string)

FlushSchema removes all entries whose key starts with prefix.

func (*Store) Get

func (s *Store) Get(key string) (any, bool)

Get retrieves a value by key.

func (*Store) GetWithMeta

func (s *Store) GetWithMeta(key string) (EntryMeta, bool)

GetWithMeta retrieves a value and its metadata.

func (*Store) Set

func (s *Store) Set(key string, value any, ttl time.Duration)

Set stores value under key with an optional TTL.

func (*Store) Stats

func (s *Store) Stats() Stats

Stats returns current statistics.

Jump to

Keyboard shortcuts

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