You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package allows to set values to a map which will expire and disappear after a specified time.
Here you can read the docs of this package, generated by pkg.go.dev.
Important
The package has been updated to v2 which will introduce breaking changes to v1.
The package now requires a minimum Go version of v1.19.
TimedMap and corresponding constructor function now take type parameters for key and value types for improved type safety.
Sections have been removed in favor of performance and simplicity of the package.
Previously deprecated functions have been removed.
If you experience issues with v1, please create an issue with the specific version mentioned. v1 will still receive updates for bugs and incosistencies alongside v2.
Usage Example
package main
import (
"log""time""github.com/zekroTJA/timedmap/v2"
)
funcmain() {
// Creates a new timed map which scans for// expired keys every 1 secondtm:= timedmap.New[string, int](1*time.Second)
// Add a key "hey" with the value 213, which should// expire after 3 seconds and execute the callback, which// prints that the key was expiredtm.Set("hey", 213, 3*time.Second, func(vint) {
log.Println("key-value pair of 'hey' has expired")
})
// Print key "hey" from timed mapprintKeyVal(tm, "hey")
// Wait for 5 seconds// During this time the main thread is blocked, the// key-value pair of "hey" will be expiredtime.Sleep(5*time.Second)
// Printing value of key "hey" wil lfail because the// key-value pair does not exist anymoreprintKeyVal(tm, "hey")
}
funcprintKeyVal(tm*timedmap.TimedMap[string, int], keystring) {
d, ok:=tm.GetValue(key)
if!ok {
log.Println("data expired")
return
}
log.Printf("%v = %d\n", key, d)
}
Further examples, you can find in the examples directory.