-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
111 lines (85 loc) · 2.7 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package cache_test
import (
"fmt"
"time"
"github.com/antichris/go-cache"
)
func ExampleNew() {
// Initialize a new cache with string keys for string values.
c := cache.New[string, string](10 * time.Millisecond)
// Since the cache checks expiration timers in an asynchronous loop
// always shut it down after use to avoid resource leaks.
defer c.Shutdown()
// A closure to output results of `Get` attempts for this demo.
show := func(k string) {
v, ok := c.Get(k)
fmt.Printf("key: %q, value: %q, present: %v\n", k, v, ok)
}
// Try with a value that is absent from the cache.
show("foo")
c.Put("bar", "baz")
// Try with the value that we just put in the cache.
show("bar")
// Wait a bit past the expiration time.
time.Sleep(15 * time.Millisecond)
// Try with the value that should be expired by now.
show("bar")
// Output:
// key: "foo", value: "", present: false
// key: "bar", value: "baz", present: true
// key: "bar", value: "", present: false
}
func ExampleNewByOf() {
type user struct {
id uint
email string
}
// A sample value.
u := user{}
// Here the cache value type is `user`, and the key type is the type
// of its `id` field. This lets you change either of those types
// without the need to touch this cache initialization.
c := cache.NewByOf(10*time.Millisecond, u.id, u)
// Since the cache checks expiration timers in an asynchronous loop
// always shut it down after use to avoid resource leaks.
defer c.Shutdown()
const ID = 1
c.Put(ID, user{
id: ID,
email: "[email protected]",
})
v, _ := c.Get(ID)
fmt.Printf("email: %s", v.email)
// Output:
// email: [email protected]
}
func ExampleCache_GetOrPut() {
type user struct {
id uint8
email string
}
// A sample value.
u := user{}
// Here the cache value type is `user`, and the key type is the type
// of its `id` field. This lets you change either of those types
// without the need to touch this cache initialization.
c := cache.NewByOf(10*time.Millisecond, u.id, u)
// Since the cache checks expiration timers in an asynchronous loop
// always shut it down after use to avoid resource leaks.
defer c.Shutdown()
getBob := func(id uint8) (user, bool) {
// We return a hard-coded value for this example, although there
// could be a call to another, more permanent storage here.
return user{
id: id,
email: "[email protected]",
}, true
}
bob, _ := c.GetOrPut(1, cache.GetterFunc[uint8, user](getBob))
fmt.Printf("email: %s", bob.email)
// Output:
// email: [email protected]
}