shardmap

package
v0.0.0-...-a4efb6c Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package shardmap is a re-done version of Josh Baker's shardmap package. It switches out the hash from xxhash to maphash, uses generics and has a few other minor changes. It is a thread-safe. Based on Josh Baker's shardmap.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Deleter

type Deleter[K comparable] func(ctx context.Context, k K) error

Deleter is a function that will be run when deleting a value from the cache. If this function returns an error, the value will not be deleted.

type Filler

type Filler[K comparable, V any] func(ctx context.Context, k K) (value *V, ok bool, err error)

Filler is a function that will be run when a value is missing from the cache. If this function returns an error, the value will not be set.

type Map

type Map[K comparable, V any] struct {
	// IsEqual is a function that determines if two values are equal. This is not required unless using
	// CompareAndSwap or CompareAndDelete.
	IsEqual func(old, new V) bool
	// contains filtered or unexported fields
}

Map is a hashmap. Like map[string]interface{}, but sharded and thread-safe.

func New

func New[K comparable, V any](less func(a, b weak.Pointer[V]) bool, maxTTL time.Duration, metrics *metrics.Cache) *Map[K, V]

New returns a new hashmap with the specified capacity. This function is only needed when you must define a minimum capacity, otherwise just use:

var m shardmap.Map

func (*Map[K, V]) CleanShards

func (m *Map[K, V]) CleanShards()

CleanShards removes all entries with nil values from the map.

func (*Map[K, V]) Clear

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

Clear out all values from map

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(ctx context.Context, key K) (prev *V, deleted bool, err error)

Delete deletes a value for a key. Returns the deleted value, or false when no value was assigned.

func (*Map[K, V]) DeleteIfMaxTTL

func (m *Map[K, V]) DeleteIfMaxTTL(key K, maxTTL time.Time) (prev *V, deleted bool)

DeleteIfMaxTTL deletes a value for a key only if the current value's TTL is less than or equal to maxTTL.

func (*Map[K, V]) DeleteIfNil

func (m *Map[K, V]) DeleteIfNil(key K) (prev *V, deleted bool)

DeleteIfNil deletes a value for a key only if the current value's weak pointer is nil.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(ctx context.Context, key K, filler Filler[K, V]) (value *V, ok bool, err error)

Get returns a value for a key. Returns false when no value has been assign for key.

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

Len returns the number of values in map. This is an approximation since keys may hold nil values that have not yet been cleaned up.

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(ctx context.Context, key K, value *V, setter Setter[K, V]) (prev *V, replaced bool, err error)

Set assigns a value to a key. Returns the previous value, or false when no value was assigned.

func (*Map[K, V]) SetIfNil

func (m *Map[K, V]) SetIfNil(ctx context.Context, key K, value *V) (setOk bool, err error)

SetIfNil assigns a value to a key only if the current value's weak pointer is nil or the key does not exist.

type Setter

type Setter[K comparable, V any] func(ctx context.Context, k K, v *V) error

Setter is a function that will be run when setting a value in the cache. If this function returns an error, the value will not be set.

Jump to

Keyboard shortcuts

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