Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zekroTJA/timedmap
A thread safe map which has expiring key-value pairs.
https://github.com/zekroTJA/timedmap
go golang hacktoberfest library map package time timemap
Last synced: 13 days ago
JSON representation
A thread safe map which has expiring key-value pairs.
- Host: GitHub
- URL: https://github.com/zekroTJA/timedmap
- Owner: zekroTJA
- License: mit
- Created: 2019-01-30T12:55:37.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-05T09:33:30.000Z (6 months ago)
- Last Synced: 2024-08-29T11:02:47.764Z (2 months ago)
- Topics: go, golang, hacktoberfest, library, map, package, time, timemap
- Language: Go
- Homepage: https://pkg.go.dev/github.com/zekroTJA/timedmap/v2
- Size: 60.5 KB
- Stars: 70
- Watchers: 4
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- awesome-go - timedmap - Map with expiring key-value pairs. (Database / Caches)
- awesome-go-extra - timedmap - value pairs.|46|7|0|2019-01-30T12:55:37Z|2022-04-29T08:41:22Z| (Generators / Caches)
README
---
go get -u github.com/zekroTJA/timedmap/v2
---
## Intro
This package allows to set values to a map which will expire and disappear after a specified time.
[Here](https://pkg.go.dev/github.com/zekroTJA/timedmap/v2) 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
```go
package mainimport (
"log"
"time""github.com/zekroTJA/timedmap/v2"
)func main() {
// Creates a new timed map which scans for
// expired keys every 1 second
tm := 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 expired
tm.Set("hey", 213, 3*time.Second, func(v int) {
log.Println("key-value pair of 'hey' has expired")
})// Print key "hey" from timed map
printKeyVal(tm, "hey")// Wait for 5 seconds
// During this time the main thread is blocked, the
// key-value pair of "hey" will be expired
time.Sleep(5 * time.Second)// Printing value of key "hey" wil lfail because the
// key-value pair does not exist anymore
printKeyVal(tm, "hey")
}func printKeyVal(tm *timedmap.TimedMap[string, int], key string) {
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](examples) directory.
If you want to see this package in a practcal use case scenario, please take a look at the rate limiter implementation of the REST API of [myrunes.com](https://myrunes.com), where I have used `timedmap` for storing client-based limiter instances:
https://github.com/myrunes/backend/blob/master/internal/ratelimit/ratelimit.go---
Copyright (c) 2020 zekro Development (Ringo Hoffmann).
Covered by MIT licence.