expirymap

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 3 Imported by: 0

README

Go Reference Github action Latest Release

ExpiryMap

This Go package provides a map that automatically removes entries after a given expiry delay.

Features

  • The map key can be any comparable type
  • The map value can be any type
  • The map is safe for concurrent use
  • The expiry delay is specified as a time.Duration value

Methods

  • New - creates a new Map
  • Get, Set, Delete - standard map operations
  • Len - returns the number of entries in the map
  • Iterate - iterates over all entries in the map
  • Clear - removes all entries from the map
  • Stop - stops the background goroutine that removes expired entries

Example

package main

import (
	"fmt"
	"time"

	"github.com/TheoBrigitte/expirymap"
)

func main() {
	// Define a key and a value.
	key := 1
	value := []string{"foo", "bar", "baz"}

	// Create a new expiry map of type map[int][]string
	// with an expiry delay of 1ns and a garbage collection interval of 1ms.
	m := expirymap.New[int, []string](time.Nanosecond, time.Millisecond)
	defer m.Stop()

	// Set 1=[foo bar baz] in the map.
	m.Set(key, value)

	fmt.Println(m.Get(1))            // [foo bar baz]
	time.Sleep(time.Millisecond * 2) // Wait for the entry to expire.
	fmt.Println(m.Get(1))            // []
}

source example/simple/simple.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Content

type Content[V any] struct {
	Data V
	// contains filtered or unexported fields
}

Content is the value stored for a given key in the ExpiryMap.

type Map

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

Map is a map of K key and V values with a builtin garbage cleaner that automatically deletes entries after a given expiry delay.

func New

func New[K comparable, V any](expiryDelay, gargabeCleanInterval time.Duration) *Map[K, V]

New returns a new ExpiryMap. It also starts a goroutine that periodically cleans up expired entries according to the expiryDelay every gargabeCleanInterval.

func (*Map[K, V]) Clear

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

Clear deletes all stored entries.

func (*Map[K, V]) Delete

func (s *Map[K, V]) Delete(key K)

Delete deletes the value for a given key.

func (*Map[K, V]) Get

func (s *Map[K, V]) Get(key K) (V, bool)

Get returns the value for a given key.

func (*Map[K, V]) Iterate

func (s *Map[K, V]) Iterate() iter.Seq2[K, V]

Iterate returns an iterator to loop over the stored entries.

func (*Map[K, V]) Len

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

Len returns the number of stored entries.

func (*Map[K, V]) Set

func (s *Map[K, V]) Set(key K, data V)

Set sets the value for a given key and reset its expiry time.

func (*Map[K, V]) Stop

func (s *Map[K, V]) Stop()

Stop stops the garbage cleaner goroutine.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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