ttlmap

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 3 Imported by: 0

README

ttlmap - Thread-safe map with expirable entries

Go Reference

This package provides a thread-safe map with expirable entries. If an entry has expired, it will be removed with the next attempt to retrieve it or by the background job.

Documentation

Overview

Package ttlmap provides thread-safe map with expirable entries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map is a map with expirable elements.

func New

func New[K comparable, V any](ttl time.Duration, numBuckets uint8) *Map[K, V]

New creates a new map with expirable elements. Whenever a key-value pair is inserted, a cleanup bucket is chosen and the inserted key-value pair is put into the bucket. With the interval of `ttl / numBuckets` a cleanup bucket is chosen and every expired element in that bucket is removed from the map (and from the bucket).

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K) (value V, exists bool)

Get retrieves an element from the map under the specified key.

func (*Map[K, V]) GetAndRemove

func (m *Map[K, V]) GetAndRemove(key K) (value V, exists bool)

GetAndRemove removes an element from the map and returns it.

func (*Map[K, V]) Has

func (m *Map[K, V]) Has(key K) bool

Has checks if an element under the specified key exists.

func (*Map[K, V]) Put

func (m *Map[K, V]) Put(key K, value V)

Put puts an element into the map.

func (*Map[K, V]) Remove

func (m *Map[K, V]) Remove(key K) bool

Remove removes an element from the map.

func (*Map[K, V]) Start

func (m *Map[K, V]) Start(ctx context.Context) (stop func())

Start starts a cleanup loop.

func (*Map[K, V]) Update

func (m *Map[K, V]) Update(key K, cb func(value V) V) bool

Update updates an existing element using provided callback function.

func (*Map[K, V]) Upsert

func (m *Map[K, V]) Upsert(key K, cb func(exists bool, value V) V)

Upsert updates an existing element or inserts a new one using provided callback function.

Jump to

Keyboard shortcuts

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