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: about 1 month 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 (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-11-29T22:42:41.000Z (6 months ago)
- Last Synced: 2024-01-30T04:06:43.529Z (4 months ago)
- Topics: go, golang, hacktoberfest, library, map, package, time, timemap
- Language: Go
- Homepage: https://pkg.go.dev/github.com/zekroTJA/timedmap
- Size: 44.9 KB
- Stars: 69
- Watchers: 4
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Lists
- awesome-go - timedmap - Map with expiring key-value pairs. (Database / Caches)
- awesome-go - timedmap - Map with expiring key-value pairs. (Database / Caches)
- awesome-go-cn - timedmap
- awesome-go-extra - timedmap - value pairs.|46|7|0|2019-01-30T12:55:37Z|2022-04-29T08:41:22Z| (Generators / Caches)
- awesome-go - timedmap - Map with expiring key-value pairs. (Database / Caches)
- awesome-go - timedmap - Map with expiring key-value pairs. (Database / Caches)
- awesome-go - timedmap - Map with expiring key-value pairs. (Data Structures / Advanced Console UIs)
- awesome-go-projects - timedmap - Map with expiring key-value pairs. (Data Structures / Standard CLI)
- awesome-go - timedmap - value pairs. | - | - | - | (Data Structures / Advanced Console UIs)
- awesome-go - timedmap - Map with expiring key-value pairs. (Database / Caches)
- go-awesome-cn-star - timedmap
- awesome-go-cn - timedmap
- awesome-go-stars - timedmap(stars: 68) - Map with expiring key-value pairs. (Database / Caches)
- awesome-go - timedmap - Map with expiring key-value pairs. (Data Structures / Advanced Console UIs)
- awesome-go-with-stars - timedmap - Map with expiring key-value pairs. (Database / Caches)
- awesome-go - timedmap - Map with expiring key-value pairs. (Data Structures / Advanced Console UIs)
- repo-1316-awesome-go-cn - timedmap
- repo-1211-awesome-go-cn - timedmap
- awesome-Char - timedmap - Map with expiring key-value pairs. (Data Structures / Advanced Console UIs)
- awesome-reader - timedmap - Map with expiring key-value pairs. (Data Structures / Advanced Console UIs)
- awesome-go - timedmap - Map with expiring key-value pairs. (Data Structures / Advanced Console UIs)
- Go-awesome - timedmap - Map with expiring key-value pairs. (Database / Caches)
- awesome-go-cn - timedmap
- awesome-go - timedmap - Map with expiring key-value pairs. (Database / Caches)
- awesome-go - timedmap - Map with expiring key-value pairs. (Data Structures / Advanced Console UIs)
- awesome-go - timedmap - Map with expiring key-value pairs. (Data Structures / Advanced Console UIs)
- awesome-go - timedmap - Map with expiring key-value pairs. (Data Structures / Advanced Console UIs)
- awesome-go-zh - timedmap
- awesome-go-cn - timedmap
- awesome-go - timedmap - Map with expiring key-value pairs. (Database / Caches)
- awesome-go - timedmap - Map with expiring key-value pairs. (Database / Caches)
README
---
go get -u github.com/zekroTJA/timedmap
---
## 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) you can read the docs of this package, generated by pkg.go.dev.
---
## Usage Example
```go
package mainimport (
"log"
"time""github.com/zekroTJA/timedmap"
)func main() {
// Create a timed map with a cleanup timer interval of 1 second
tm := timedmap.New(1 * time.Second)
// Set value of key "hey" to 213, which will expire after 3 seconds
tm.Set("hey", 213, 3*time.Second)
// Print the value of "hey"
printKeyVal(tm, "hey")
// Block the main thread for 5 seconds
// After this time, the key-value pair "hey": 213 has expired
time.Sleep(5 * time.Second)
// Now, this function should show that there is no key "hey"
// in the map, because it has been expired
printKeyVal(tm, "hey")
}func printKeyVal(tm *timedmap.TimedMap, key interface{}) {
d, ok := tm.GetValue(key).(int)
if !ok {
log.Println("data expired")
return
}log.Printf("%v = %d\n", key, d)
}
```Further examples, you can find in the [example](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.