forked from Code-Hex/go-generics-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
131 lines (114 loc) · 2.53 KB
/
example_test.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
121
122
123
124
125
126
127
128
129
130
131
package cache_test
import (
"context"
"fmt"
"time"
cache "github.com/gekatateam/go-generics-cache"
)
func ExampleCache() {
// use simple cache algorithm without options.
c := cache.New[string, int]()
c.Set("a", 1)
gota, aok := c.Get("a")
gotb, bok := c.Get("b")
fmt.Println(gota, aok)
fmt.Println(gotb, bok)
// Output:
// 1 true
// 0 false
}
func ExampleNewContext() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// use simple cache algorithm without options.
// an internal janitor will be stopped if specified the context is cancelled.
c := cache.NewContext(ctx, cache.WithJanitorInterval[string, int](3*time.Second))
c.Set("a", 1)
gota, aok := c.Get("a")
gotb, bok := c.Get("b")
fmt.Println(gota, aok)
fmt.Println(gotb, bok)
// Output:
// 1 true
// 0 false
}
func ExampleAsClock() {
// use clock cache algorithm.
c := cache.New(cache.AsClock[string, int]())
c.Set("a", 1)
gota, aok := c.Get("a")
gotb, bok := c.Get("b")
fmt.Println(gota, aok)
fmt.Println(gotb, bok)
// Output:
// 1 true
// 0 false
}
func ExampleWithExpiration() {
c := cache.New(cache.AsFIFO[string, int]())
exp := 250 * time.Millisecond
c.Set("a", 1, cache.WithExpiration(exp))
// check item is set.
gota, aok := c.Get("a")
fmt.Println(gota, aok)
// set again
c.Set("a", 2, cache.WithExpiration(exp))
gota2, aok2 := c.Get("a")
fmt.Println(gota2, aok2)
// waiting expiration.
time.Sleep(exp + 100*time.Millisecond) // + buffer
gota3, aok3 := c.Get("a") // expired
fmt.Println(gota3, aok3)
// Output:
// 1 true
// 2 true
// 0 false
}
func ExampleCache_Delete() {
c := cache.New(cache.AsMRU[string, int]())
c.Set("a", 1)
c.Delete("a")
gota, aok := c.Get("a")
fmt.Println(gota, aok)
// Output:
// 0 false
}
func ExampleCache_Keys() {
c := cache.New(cache.AsLFU[string, int]())
c.Set("a", 1)
c.Set("b", 1)
c.Set("c", 1)
fmt.Println(c.Keys())
// Output:
// [a b c]
}
func ExampleCache_Contains() {
c := cache.New(cache.AsLRU[string, int]())
c.Set("a", 1)
fmt.Println(c.Contains("a"))
fmt.Println(c.Contains("b"))
// Output:
// true
// false
}
func ExampleNewNumber() {
nc := cache.NewNumber[string, int]()
nc.Set("a", 1)
nc.Set("b", 2, cache.WithExpiration(time.Minute))
av := nc.Increment("a", 1)
gota, aok := nc.Get("a")
bv := nc.Decrement("b", 1)
gotb, bok := nc.Get("b")
// not set keys
cv := nc.Increment("c", 100)
dv := nc.Decrement("d", 100)
fmt.Println(av, gota, aok)
fmt.Println(bv, gotb, bok)
fmt.Println(cv)
fmt.Println(dv)
// Output:
// 2 2 true
// 1 1 true
// 100
// -100
}