sharded

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package sharded implements a high‑throughput, zero‑allocation sharded map intended for in‑memory cache workloads. Hot paths (Get/Set/Remove) avoid allocations and keep critical sections short. Global counters are atomics so they can be read without locks.

Index

Constants

View Source
const (
	NumOfShards = 1024
)

Tunables.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRUMode added in v1.6.0

type LRUMode int
const (
	Listing LRUMode = iota
	Sampling
)

type Map

type Map[V Value] struct {
	// contains filtered or unexported fields
}

Map is a sharded concurrent map with precise global counters.

func NewMap

func NewMap[V Value](ctx context.Context, cfg config.Config) *Map[V]

NewMap creates the map and initializes shards. A lightweight gauge updater runs once per second and exits with ctx.

func (*Map[V]) AddMem added in v1.6.0

func (m *Map[V]) AddMem(key uint64, delta int64)

func (*Map[V]) Clear added in v1.4.0

func (m *Map[V]) Clear()

Clear wipes all shards and fixes global counters atomically.

func (*Map[V]) EnqueueExpired added in v1.7.0

func (m *Map[V]) EnqueueExpired(key uint64) bool

EnqueueExpired tries to put key to its shard refresh queue.

func (*Map[V]) EvictUntilWithinLimit added in v1.6.0

func (m *Map[V]) EvictUntilWithinLimit(limit, backoff int64) (freed, evicted int64)

func (*Map[V]) Get

func (m *Map[V]) Get(key uint64) (value V, ok bool)

Get reads a value.

func (*Map[V]) Len

func (m *Map[V]) Len() int64

func (*Map[V]) Mem

func (m *Map[V]) Mem() int64

func (*Map[V]) NextQueuedWithExpiredTTL added in v1.7.0

func (m *Map[V]) NextQueuedWithExpiredTTL() (V, bool)

NextQueuedWithExpiredTTL tries to pop one queued key from up to 'probes' shards.

func (*Map[V]) NextShard added in v1.4.4

func (m *Map[V]) NextShard() *Shard[V]

func (*Map[V]) PeekExpiredTTL added in v1.7.0

func (m *Map[V]) PeekExpiredTTL() (V, bool)

func (*Map[V]) PickVictim added in v1.6.0

func (m *Map[V]) PickVictim(shardsSample, keysSample int64) (bestShard *Shard[V], victim V, ok bool)

func (*Map[V]) Remove

func (m *Map[V]) Remove(key uint64) (freedBytes int64, hit bool)

Remove deletes a key and adjusts global counters.

func (*Map[V]) Set

func (m *Map[V]) Set(key uint64, value V)

Set inserts/updates a value and adjusts global counters via per‑shard deltas.

func (*Map[V]) Shard

func (m *Map[V]) Shard(key uint64) *Shard[V]

func (*Map[V]) Touch added in v1.6.0

func (m *Map[V]) Touch(key uint64)

func (*Map[V]) UsingListing added in v1.6.0

func (m *Map[V]) UsingListing() bool

func (*Map[V]) UsingSampling added in v1.6.0

func (m *Map[V]) UsingSampling() bool

func (*Map[V]) WalkShards

func (m *Map[V]) WalkShards(ctx context.Context, fn func(key uint64, shard *Shard[V]))

WalkShards applies fn to all shards synchronously (zero-alloc).

func (*Map[V]) WalkShardsConcurrent added in v1.6.0

func (m *Map[V]) WalkShardsConcurrent(ctx context.Context, concurrency int, fn func(key uint64, shard *Shard[V]))

WalkShardsConcurrent executes fn over shards with bounded concurrency. Use in maintenance/background tasks; avoid on hot paths.

type Shard

type Shard[V Value] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Shard is an independent segment of the sharded map. It keeps per-shard counters read with atomics so global readers can avoid locks.

func NewShard

func NewShard[V Value](id uint64) *Shard[V]

NewShard creates a shard with small map capacity and fixed-size reservoirs.

func (*Shard[V]) AddMem added in v1.6.0

func (sh *Shard[V]) AddMem(delta int64)

func (*Shard[V]) Clear added in v0.9.7

func (sh *Shard[V]) Clear() (freedBytes int64, items int64)

Clear removes all entries and returns (freedBytes, itemsRemoved). Reservoirs are kept intact; stale keys are naturally validated&skipped.

func (*Shard[V]) DequeueExpired added in v1.7.0

func (sh *Shard[V]) DequeueExpired() (uint64, bool)

DequeueExpired remove key from refresh bucket

func (*Shard[V]) EnqueueRefresh added in v1.7.0

func (sh *Shard[V]) EnqueueRefresh(key uint64) bool

EnqueueRefresh insert key into refresh bucket

func (*Shard[V]) Get

func (sh *Shard[V]) Get(key uint64) (value V, hit bool)

Get reads a value under a shared lock.

func (*Shard[V]) ID

func (sh *Shard[V]) ID() uint64

func (*Shard[V]) Len

func (sh *Shard[V]) Len() int64

func (*Shard[V]) Remove

func (sh *Shard[V]) Remove(key uint64) (freedBytes int64, hit bool)

Remove deletes a key under the write lock.

func (*Shard[V]) RemoveUnlocked added in v1.6.0

func (sh *Shard[V]) RemoveUnlocked(key uint64) (freedBytes int64, hit bool)

RemoveUnlocked deletes a key when the shard is already exclusively locked.

func (*Shard[V]) Set

func (sh *Shard[V]) Set(key uint64, new V) (bytesDelta int64, lenDelta int64)

Set inserts or updates a key. Returns deltas for global aggregations.

func (*Shard[V]) Walk

func (sh *Shard[V]) Walk(ctx context.Context, fn func(uint64, V) bool, write bool)

Walk iterates (k,v) under a shared lock. The callback must be lightweight.

func (*Shard[V]) WalkR added in v1.4.4

func (sh *Shard[V]) WalkR(ctx context.Context, fn func(uint64, V) bool)

WalkR iterates (k,v) under a shared lock. The callback must be lightweight.

func (*Shard[V]) WalkW added in v1.4.4

func (sh *Shard[V]) WalkW(ctx context.Context, fn func(uint64, V) bool)

WalkW iterates under the write lock. Use with care.

func (*Shard[V]) Weight

func (sh *Shard[V]) Weight() int64

type Value

type Value interface {
	Key() uint64
	Weight() int64
	IsExpired(cfg config.Config) bool
	ClearRefreshQueued()
	TouchedAt() int64
	FreshAt() int64
}

Value identifies the requirements for values stored in the sharded map. All methods must be O(1) and allocation‑free.

Jump to

Keyboard shortcuts

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