A generic set with time based eviction.
This set uses generics to contain any type of comparable element, evicting elements after a given expiration period.
Create and start the set then add some items with time to live
import "github.com/logbn/expset"
s := expset.New[string]()
s.Start()
defer s.Stop()
s.Add("test-1", 10 * time.Second)
s.Add("test-2", 20 * time.Second)
s.Add("test-3", 30 * time.Second)
println(s.Len())
// output:
// 3Test whether set Has a value
println(s.Has("test-1"))
// output:
// true
println(s.Has("test-2"))
// output:
// trueObserve expiration
time.Sleep(10*time.Second)
println(s.Has("test-1"))
// output:
// false
println(s.Has("test-2"))
// output:
// trueRefresh an item to reset its expiration
s.Refresh("test-2")
time.Sleep(10*time.Second)
println(s.Has("test-2"))
// output:
// trueThis package is thread safe.
Expiring Set is licensed under Apache 2.0