Skip to content

Commit

Permalink
Merge pull request #9 from zekroTJA/v2
Browse files Browse the repository at this point in the history
v2 update
  • Loading branch information
zekroTJA authored May 4, 2024
2 parents 5a95c12 + d3aff47 commit 0f4ecf1
Show file tree
Hide file tree
Showing 19 changed files with 416 additions and 711 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/main-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.13, 1.14, 1.15, ^1.16]
go-version: ["1.19", "1.20", "1.21", "^1.22"]
steps:

- name: Set up Go 1.x
Expand All @@ -28,6 +28,9 @@ jobs:
run: |
go get -v -t -d ./...
- name: Run Tests with Race checks
run: go test -v -timeout 300s -race ./...

- name: Run Tests
run: go test -v -timeout 300s -covermode atomic -coverprofile=covprofile ./...

Expand Down
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/git_toolbox_prj.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/timedmap.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## v2.0.0

- Add type parameters for the key and value type of a TimedMap and corresponding constructor functions.
- Remove the section system for better simplicity and usability.
- Remove deprecated `SetExpire` method.
- Update documentation.
- Update minimum required Go version to v1.19.0.

## v1.5.2

- Multiple race conditions have been fixed (by @ShivamKumar2002 in https://github.com/zekroTJA/timedmap/pull/8)

## v1.5.1

- Add [`FromMap`](https://pkg.go.dev/github.com/zekroTJA/timedmap#FromMap) constructor which can be used to create a `TimedMap` from an existing map with the given expiration values for each key-value pair.

## v1.4.0

- Add `SetExpires` method to match `Section` interface and match naming scheme of the other expire-related endpoints.
Expand Down
50 changes: 35 additions & 15 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,16 @@

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.

> [!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`.
---

Expand All @@ -34,26 +43,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 All @@ -63,7 +83,7 @@ func printKeyVal(tm *timedmap.TimedMap, key interface{}) {
}
```

Further examples, you can find in the [example](examples) directory.
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
Expand Down
46 changes: 46 additions & 0 deletions benchmarks/v1.5.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
goos: windows
goarch: amd64
pkg: github.com/zekroTJA/timedmap
cpu: AMD Ryzen 7 5800X 8-Core Processor
BenchmarkSetValues-16 1882472 678.8 ns/op 252 B/op 3 allocs/op
BenchmarkSetValues-16 1968472 670.1 ns/op 245 B/op 3 allocs/op
BenchmarkSetValues-16 2001894 655.4 ns/op 242 B/op 3 allocs/op
BenchmarkSetValues-16 2135914 635.2 ns/op 232 B/op 3 allocs/op
BenchmarkSetValues-16 2148526 631.1 ns/op 231 B/op 3 allocs/op
BenchmarkSetValues-16 2146839 633.8 ns/op 231 B/op 3 allocs/op
BenchmarkSetValues-16 2160771 630.3 ns/op 230 B/op 3 allocs/op
BenchmarkSetValues-16 2159397 634.7 ns/op 230 B/op 3 allocs/op
BenchmarkSetValues-16 2115090 636.0 ns/op 233 B/op 3 allocs/op
BenchmarkSetValues-16 2125656 633.5 ns/op 232 B/op 3 allocs/op
BenchmarkSetGetValues-16 1862566 729.9 ns/op 254 B/op 3 allocs/op
BenchmarkSetGetValues-16 1837540 723.3 ns/op 256 B/op 3 allocs/op
BenchmarkSetGetValues-16 1820694 702.8 ns/op 258 B/op 3 allocs/op
BenchmarkSetGetValues-16 1830153 708.0 ns/op 257 B/op 3 allocs/op
BenchmarkSetGetValues-16 1831124 716.2 ns/op 257 B/op 3 allocs/op
BenchmarkSetGetValues-16 1837466 709.6 ns/op 256 B/op 3 allocs/op
BenchmarkSetGetValues-16 1820491 710.9 ns/op 258 B/op 3 allocs/op
BenchmarkSetGetValues-16 1851866 724.8 ns/op 255 B/op 3 allocs/op
BenchmarkSetGetValues-16 1834436 720.6 ns/op 257 B/op 3 allocs/op
BenchmarkSetGetValues-16 1760607 670.2 ns/op 264 B/op 3 allocs/op
BenchmarkSetGetRemoveValues-16 3977371 300.9 ns/op 15 B/op 1 allocs/op
BenchmarkSetGetRemoveValues-16 3949048 300.1 ns/op 16 B/op 1 allocs/op
BenchmarkSetGetRemoveValues-16 3930261 301.7 ns/op 15 B/op 1 allocs/op
BenchmarkSetGetRemoveValues-16 3964603 302.3 ns/op 15 B/op 1 allocs/op
BenchmarkSetGetRemoveValues-16 3970574 301.3 ns/op 15 B/op 1 allocs/op
BenchmarkSetGetRemoveValues-16 3970753 303.3 ns/op 16 B/op 1 allocs/op
BenchmarkSetGetRemoveValues-16 3957939 302.4 ns/op 16 B/op 1 allocs/op
BenchmarkSetGetRemoveValues-16 3965145 301.8 ns/op 16 B/op 1 allocs/op
BenchmarkSetGetRemoveValues-16 3981002 302.6 ns/op 15 B/op 1 allocs/op
BenchmarkSetGetRemoveValues-16 3960450 301.5 ns/op 16 B/op 1 allocs/op
BenchmarkSetGetSameKey-16 8956666 132.5 ns/op 8 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 9140398 132.4 ns/op 8 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 9072358 133.2 ns/op 8 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 9228426 130.8 ns/op 8 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 9120446 132.2 ns/op 8 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 9067176 132.3 ns/op 8 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 9152479 132.0 ns/op 8 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 9019464 131.8 ns/op 8 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 9069512 131.6 ns/op 8 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 8987463 133.1 ns/op 8 B/op 0 allocs/op
PASS
ok github.com/zekroTJA/timedmap 575.199s
46 changes: 46 additions & 0 deletions benchmarks/v2.0.0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
goos: windows
goarch: amd64
pkg: github.com/zekroTJA/timedmap
cpu: AMD Ryzen 7 5800X 8-Core Processor
BenchmarkSetValues-16 3184940 339.4 ns/op 120 B/op 1 allocs/op
BenchmarkSetValues-16 3601794 438.6 ns/op 159 B/op 1 allocs/op
BenchmarkSetValues-16 3893646 375.8 ns/op 152 B/op 1 allocs/op
BenchmarkSetValues-16 4121847 372.0 ns/op 147 B/op 1 allocs/op
BenchmarkSetValues-16 4067539 368.1 ns/op 148 B/op 1 allocs/op
BenchmarkSetValues-16 4130247 367.4 ns/op 147 B/op 1 allocs/op
BenchmarkSetValues-16 3752602 376.5 ns/op 155 B/op 1 allocs/op
BenchmarkSetValues-16 3900349 372.1 ns/op 151 B/op 1 allocs/op
BenchmarkSetValues-16 4099213 373.1 ns/op 147 B/op 1 allocs/op
BenchmarkSetValues-16 4141642 371.3 ns/op 146 B/op 1 allocs/op
BenchmarkSetGetValues-16 3391387 406.2 ns/op 117 B/op 1 allocs/op
BenchmarkSetGetValues-16 3330207 389.4 ns/op 118 B/op 1 allocs/op
BenchmarkSetGetValues-16 3308163 381.0 ns/op 118 B/op 1 allocs/op
BenchmarkSetGetValues-16 3349912 383.9 ns/op 118 B/op 1 allocs/op
BenchmarkSetGetValues-16 3350439 382.1 ns/op 118 B/op 1 allocs/op
BenchmarkSetGetValues-16 3322348 384.1 ns/op 118 B/op 1 allocs/op
BenchmarkSetGetValues-16 3327039 384.1 ns/op 118 B/op 1 allocs/op
BenchmarkSetGetValues-16 3311404 385.7 ns/op 118 B/op 1 allocs/op
BenchmarkSetGetValues-16 3230353 402.7 ns/op 119 B/op 1 allocs/op
BenchmarkSetGetValues-16 3298658 387.8 ns/op 118 B/op 1 allocs/op
BenchmarkSetGetRemoveValues-16 8977050 133.6 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetRemoveValues-16 8609907 134.7 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetRemoveValues-16 8937481 133.7 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetRemoveValues-16 8970057 135.2 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetRemoveValues-16 9006141 133.3 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetRemoveValues-16 8969152 134.2 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetRemoveValues-16 8832172 133.9 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetRemoveValues-16 8906787 133.8 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetRemoveValues-16 9071329 136.9 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetRemoveValues-16 8991464 132.3 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 17527305 69.23 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 17328144 68.63 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 17363221 68.14 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 17746070 69.00 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 17612041 69.11 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 17479461 69.11 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 17608268 68.99 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 17113422 67.34 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 17349764 69.17 ns/op 0 B/op 0 allocs/op
BenchmarkSetGetSameKey-16 17481498 68.93 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/zekroTJA/timedmap/v2 621.443s
10 changes: 5 additions & 5 deletions examples/default/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import (
"log"
"time"

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

func main() {

// Creates a new timed map which scans for
// expired keys every 1 second
tm := timedmap.New(1 * time.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 interface{}) {
tm.Set("hey", 213, 3*time.Second, func(v int) {
log.Println("key-value pair of 'hey' has expired")
})

Expand All @@ -33,8 +33,8 @@ func main() {
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
68 changes: 0 additions & 68 deletions examples/sections/sections.go

This file was deleted.

Loading

0 comments on commit 0f4ecf1

Please sign in to comment.