Skip to content

logbn/expset

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Expiring Set

A generic set with time based eviction.

Go Reference License Go Report Card Go Coverage

This set uses generics to contain any type of comparable element, evicting elements after a given expiration period.

Usage

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:
//   3

Test whether set Has a value

println(s.Has("test-1"))
// output:
//   true

println(s.Has("test-2"))
// output:
//   true

Observe expiration

time.Sleep(10*time.Second)

println(s.Has("test-1"))
// output:
//   false

println(s.Has("test-2"))
// output:
//   true

Refresh an item to reset its expiration

s.Refresh("test-2")

time.Sleep(10*time.Second)

println(s.Has("test-2"))
// output:
//   true

Concurrency

This package is thread safe.

License

Expiring Set is licensed under Apache 2.0

About

Generic Expiring Set in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published