-
Notifications
You must be signed in to change notification settings - Fork 6
/
example.go
120 lines (102 loc) · 3.48 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
example.go
-John Taylor
2023-10-21
This is an example on how to use the TtlMap package. Notice the variety of data types used.
*/
package main
import (
"fmt"
"time"
"github.com/jftuga/TtlMap"
)
type User struct {
Name string
Level uint
}
func main() {
maxTTL := time.Duration(time.Second * 4) // time in seconds
startSize := 3 // initial number of items in map
pruneInterval := time.Duration(time.Second * 1) // search for expired items every 'pruneInterval' seconds
refreshLastAccessOnGet := true // update item's lastAccessTime on a .Get()
t := TtlMap.New[string](maxTTL, startSize, pruneInterval, refreshLastAccessOnGet)
defer t.Close()
// populate the TtlMap
t.Put("string", "a b c")
t.Put("int", 3)
t.Put("float", 4.4)
t.Put("int_array", []int{1, 2, 3})
t.Put("bool", false)
t.Put("rune", '{')
t.Put("byte", 0x7b)
var u = uint64(123456789)
t.Put("uint64", u)
var c = complex(3.14, -4.321)
t.Put("complex", c)
allUsers := []User{{Name: "abc", Level: 123}, {Name: "def", Level: 456}}
t.Put("all_users", allUsers)
fmt.Println()
fmt.Println("TtlMap length:", t.Len())
// extract entry from struct array
a := t.Get("all_users").([]User)
fmt.Printf("second user: %v, %v\n", a[1].Name, a[1].Level)
// display all items in TtlMap
fmt.Println()
fmt.Println("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
all := t.All()
for k, v := range all {
fmt.Printf("[%9s] %v\n", k, v.Value)
}
fmt.Println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
fmt.Println()
// by executing Get(), the 'dontExpireKey' lastAccessTime will be updated
// therefore, this item will not expire
dontExpireKey := "float"
go func() {
for range time.Tick(time.Second) {
t.Get(dontExpireKey)
}
}()
// TtlMap has an expiration time, wait until this amount of time passes
sleepTime := maxTTL + pruneInterval
fmt.Println()
fmt.Printf("Sleeping %v seconds, items should be removed after this time, except for the '%v' key\n", sleepTime, dontExpireKey)
fmt.Println()
time.Sleep(sleepTime)
// these items have expired and therefore should be nil, except for 'dontExpireKey'
fmt.Printf("[%9s] %v\n", "string", t.Get("string"))
fmt.Printf("[%9s] %v\n", "int", t.Get("int"))
fmt.Printf("[%9s] %v\n", "float", t.Get("float"))
fmt.Printf("[%9s] %v\n", "int_array", t.Get("int_array"))
fmt.Printf("[%9s] %v\n", "bool", t.Get("bool"))
fmt.Printf("[%9s] %v\n", "rune", t.Get("rune"))
fmt.Printf("[%9s] %v\n", "byte", t.Get("byte"))
fmt.Printf("[%9s] %v\n", "uint64", t.Get("uint64"))
fmt.Printf("[%9s] %v\n", "complex", t.Get("complex"))
fmt.Printf("[%9s] %v\n", "all_users", t.Get("all_users"))
// sanity check, this comparison should be true
fmt.Println()
if t.Get("int") == nil {
fmt.Println("[int] is nil")
}
fmt.Println("TtlMap length:", t.Len(), " (should equal 1)")
fmt.Println()
fmt.Println()
fmt.Printf("Manually deleting '%v' key; should be successful\n", dontExpireKey)
success := t.Delete(dontExpireKey)
fmt.Printf(" successful? %v\n", success)
fmt.Printf("Manually deleting '%v' key again; should NOT be successful this time\n", dontExpireKey)
success = t.Delete(dontExpireKey)
fmt.Printf(" successful? %v\n", success)
fmt.Println("TtlMap length:", t.Len(), " (should equal 0)")
fmt.Println()
fmt.Println("Adding 2 items and then running Clear()")
t.Put("string", "a b c")
t.Put("int", 3)
fmt.Println("TtlMap length:", t.Len())
fmt.Println()
fmt.Println("Running Clear()")
t.Clear()
fmt.Println("TtlMap length:", t.Len())
fmt.Println()
}