-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
example_test.go
81 lines (74 loc) · 1.31 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
package simple_test
import (
"fmt"
"testing"
"github.com/Code-Hex/go-generics-cache/policy/simple"
)
func ExampleNewCache() {
c := simple.NewCache[string, int]()
c.Set("a", 1)
c.Set("b", 2)
av, aok := c.Get("a")
bv, bok := c.Get("b")
cv, cok := c.Get("c")
fmt.Println(av, aok)
fmt.Println(bv, bok)
fmt.Println(cv, cok)
c.Delete("a")
_, aok2 := c.Get("a")
if !aok2 {
fmt.Println("key 'a' has been deleted")
}
// update
c.Set("b", 3)
newbv, _ := c.Get("b")
fmt.Println(newbv)
// Output:
// 1 true
// 2 true
// 0 false
// key 'a' has been deleted
// 3
}
func ExampleCache_Keys() {
c := simple.NewCache[string, int]()
c.Set("foo", 1)
c.Set("bar", 2)
c.Set("baz", 3)
keys := c.Keys()
for _, key := range keys {
fmt.Println(key)
}
// Output:
// foo
// bar
// baz
}
func ExampleCache_Len() {
c := simple.NewCache[string, int]()
c.Set("foo", 1)
c.Set("bar", 2)
c.Set("baz", 3)
len := c.Len()
fmt.Println(len)
// Output:
// 3
}
func BenchmarkLenWithKeys(b *testing.B) {
c := simple.NewCache[string, int]()
c.Set("foo", 1)
c.Set("bar", 2)
c.Set("baz", 3)
for i := 0; i < b.N; i++ {
var _ = len(c.Keys())
}
}
func BenchmarkJustLen(b *testing.B) {
c := simple.NewCache[string, int]()
c.Set("foo", 1)
c.Set("bar", 2)
c.Set("baz", 3)
for i := 0; i < b.N; i++ {
var _ = c.Len()
}
}