This Go package provides a map that automatically removes entries after a given expiry delay.
- 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
New
- creates a newMap
Get
,Set
,Delete
- standard map operationsLen
- returns the number of entries in the mapIterate
- iterates over all entries in the mapClear
- removes all entries from the mapStop
- stops the background goroutine that removes expired entries
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