Skip to content

Commit

Permalink
add benchmarks; update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed May 4, 2024
1 parent c38dbd0 commit 8094c45
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
---

<div align="center">
<code>go get -u github.com/zekroTJA/timedmap</code>
<code>go get -u github.com/zekroTJA/timedmap/v2</code>
</div>

---
Expand All @@ -21,7 +21,7 @@

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.
[Here](https://pkg.go.dev/github.com/zekroTJA/timedmap/v2) you can read the docs of this package, generated by pkg.go.dev.

---

Expand All @@ -34,26 +34,37 @@ import (
"log"
"time"

"github.com/zekroTJA/timedmap"
"github.com/zekroTJA/timedmap/v2"
)

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"

// 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")
// Block the main thread for 5 seconds
// After this time, the key-value pair "hey": 213 has expired

// 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)
// Now, this function should show that there is no key "hey"
// in the map, because it has been expired

// Printing value of key "hey" wil lfail because the
// key-value pair does not exist anymore
printKeyVal(tm, "hey")
}

func printKeyVal(tm *timedmap.TimedMap, key interface{}) {
d, ok := tm.GetValue(key).(int)
func printKeyVal(tm *timedmap.TimedMap[string, int], key string) {
d, ok := tm.GetValue(key)
if !ok {
log.Println("data expired")
return
Expand Down

0 comments on commit 8094c45

Please sign in to comment.