Skip to content

Commit

Permalink
Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoBrigitte committed Nov 14, 2024
1 parent 4cbb8d9 commit 8913fb4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ This Go package provides a map that automatically removes entries after a given

## Example

See [example/main.go](./example/main.go)
See [example/simple/simple.go](./example/simple/simple.go)
24 changes: 24 additions & 0 deletions example/iterator/iterator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"time"

"github.com/TheoBrigitte/expirymap"
)

func main() {
// Create a new map[int]string with an expiry delay of 5s and a garbage collection interval of 1s.
m := expirymap.New[int, string](time.Second*5, time.Second)
defer m.Stop()

// Set 1=foo, 2=bar, 3=baz in the map.
m.Set(1, "foo")
m.Set(2, "bar")
m.Set(3, "baz")

// Iterate over the map and print the key and value.
for k, v := range m.Iterate() {
fmt.Println(k, v)
}
}
25 changes: 25 additions & 0 deletions example/simple/simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"time"

"github.com/TheoBrigitte/expirymap"
)

func main() {
// Define a key and a value.
key := 1
value := []string{"foo", "bar", "baz"}

// Create a new 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)) // []
}

0 comments on commit 8913fb4

Please sign in to comment.