forked from deanishe/awgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_examples_test.go
188 lines (150 loc) · 3.55 KB
/
cache_examples_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2018 Dean Jackson <[email protected]>
// MIT Licence - http://opensource.org/licenses/MIT
package aw
import (
"fmt"
"io/ioutil"
"os"
"time"
)
func ExampleCache() {
var (
// Cache "key" (filename) and the value to store
name = "LastOpened"
value = time.Now()
)
// Create a temporary directory for Cache to use
dir, err := ioutil.TempDir("", "awgo-demo")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
// Create a new cache
c := NewCache(dir)
// Cache doesn't exist yet
fmt.Println(c.Exists(name)) // -> false
// The API uses bytes
data, _ := value.MarshalText()
if err := c.Store(name, data); err != nil {
panic(err)
}
// Cache now exists
fmt.Println(c.Exists(name)) // -> true
// Load data from cache
data, err = c.Load(name)
if err != nil {
panic(err)
}
v2 := time.Time{}
_ = v2.UnmarshalText(data)
// Values are equal
fmt.Println(value.Equal(v2)) // -> true
// Output:
// false
// true
// true
}
// LoadOrStore loads data from cache if they're fresh enough, otherwise it calls
// the reload function for new data (which it caches).
func ExampleCache_LoadOrStore() {
var (
name = "Expiring"
data = []byte("test")
maxAge = time.Millisecond * 1000
start = time.Now()
reloadCount int
)
// Create a temporary directory for Cache to use
dir, err := ioutil.TempDir("", "awgo-demo")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
// Create a new cache
c := NewCache(dir)
// Called by LoadOrStore when cache is empty or has expired
reload := func() ([]byte, error) {
// Log call count
reloadCount++
fmt.Printf("reload #%d\n", reloadCount)
return data, nil
}
// Cache is empty
fmt.Println(c.Exists(name)) // -> false
out, err := c.LoadOrStore(name, maxAge, reload)
if err != nil {
panic(err)
}
// Reload was called and cache exists
fmt.Println(c.Exists(name)) // -> true
// Value is the same
fmt.Println(string(out) == string(data)) // -> true
// Load again, this time from cache, not reload
out, err = c.LoadOrStore(name, maxAge, reload)
if err != nil {
panic(err)
}
// Value is the same
fmt.Println(string(out) == string(data)) // -> true
// Wait for cache to expire, then try again
time.Sleep(time.Millisecond + maxAge - time.Since(start))
// reload is called again
out, err = c.LoadOrStore(name, maxAge, reload)
if err != nil {
panic(err)
}
// Value is the same
fmt.Println(string(out) == string(data)) // -> true
// Output:
// false
// reload #1
// true
// true
// true
// reload #2
// true
}
// LoadOrStoreJSON marshals JSON to/from the cache.
func ExampleCache_LoadOrStoreJSON() {
var (
name = "Host"
maxAge = time.Second * 5
)
// Create a temporary directory for Cache to use
dir, err := ioutil.TempDir("", "awgo-demo")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
// struct to cache
type host struct {
Hostname string
Port int
}
// Called by LoadOrStoreJSON. Returns default host.
// Normally, this function would do something that takes some time, like
// fetch data from the web or an application.
reload := func() (interface{}, error) {
fmt.Println("reload")
return &host{
Hostname: "localhost",
Port: 6000,
}, nil
}
// Create a new cache
c := NewCache(dir)
// Cache is empty
fmt.Println(c.Exists(name)) // -> false
// Populate new host from cache/reload
h := &host{}
if err := c.LoadOrStoreJSON(name, maxAge, reload, h); err != nil {
panic(err)
}
fmt.Println(h.Hostname)
fmt.Println(h.Port)
// Output:
// false
// reload
// localhost
// 6000
}