https://github.com/theobrigitte/expirymap
Golang expirable map package
https://github.com/theobrigitte/expirymap
go golang
Last synced: 4 months ago
JSON representation
Golang expirable map package
- Host: GitHub
- URL: https://github.com/theobrigitte/expirymap
- Owner: TheoBrigitte
- Created: 2024-10-16T09:36:17.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-10-16T13:06:24.000Z (9 months ago)
- Last Synced: 2024-10-19T07:06:28.623Z (9 months ago)
- Topics: go, golang
- Language: Go
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## 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
```go
package mainimport (
"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](./example/simple/simple.go)