/
adapters_example_test.go
245 lines (192 loc) · 5.14 KB
/
adapters_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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package cacher_test
import (
"fmt"
"github.com/jelmersnoeck/cacher"
"github.com/jelmersnoeck/cacher/internal/encoding"
)
func ExampleAdd() {
cacher.Flush()
ok1 := cacher.Add("key1", []byte("value1"), 0)
val1, _, _ := cacher.Get("key1")
fmt.Println(string(val1), ok1)
ok2 := cacher.Add("key1", []byte("value2"), 0)
val2, _, _ := cacher.Get("key1")
fmt.Println(string(val2), ok2)
// Output:
// value1 <nil>
// value1 Key `key1` already exists.
}
func ExampleSet() {
cacher.Flush()
ok1 := cacher.Set("key1", []byte("value1"), 0)
val1, _, _ := cacher.Get("key1")
fmt.Println(string(val1), ok1)
ok2 := cacher.Set("key1", []byte("value2"), 0)
val2, _, _ := cacher.Get("key1")
fmt.Println(string(val2), ok2)
// Output:
// value1 <nil>
// value2 <nil>
}
func ExampleSetMulti() {
cacher.Flush()
multi := map[string][]byte{
"key1": []byte("value1"),
"key2": []byte("value2"),
}
cacher.SetMulti(multi, 0)
val1, _, _ := cacher.Get("key1")
val2, _, _ := cacher.Get("key2")
fmt.Println(string(val1))
fmt.Println(string(val2))
// Output:
// value1
// value2
}
func ExampleCompareAndReplace() {
cacher.Flush()
cacher.Set("key1", []byte("hello world"), 0)
_, token, _ := cacher.Get("key1")
var err error
var val []byte
err = cacher.CompareAndReplace(token+"FALSE", "key1", []byte("replacement1"), 0)
val, _, _ = cacher.Get("key1")
fmt.Println(err, string(val))
err = cacher.CompareAndReplace(token, "key1", []byte("replacement2"), 0)
val, _, _ = cacher.Get("key1")
fmt.Println(err, string(val))
// Output:
// Key `key1` does not exist. hello world
// <nil> replacement2
}
func ExampleReplace() {
cacher.Flush()
var err error
err = cacher.Replace("key1", []byte("replacement"), 0)
fmt.Println(err)
cacher.Set("key1", []byte("value1"), 0)
err = cacher.Replace("key1", []byte("replacement"), 0)
fmt.Println(err)
// Output:
// Key `key1` does not exist.
// <nil>
}
func ExampleGet() {
cacher.Flush()
var value []byte
var token string
var err error
value, token, err = cacher.Get("non-existing")
fmt.Println(string(value), token, err)
cacher.Set("key1", []byte("Hello world!"), 0)
value, token, err = cacher.Get("key1")
fmt.Println(string(value), token, err)
// Output:
// Key `non-existing` does not exist.
// Hello world! 86fb269d190d2c85f6e0468ceca42a20 <nil>
}
func ExampleGetMulti() {
cacher.Flush()
multi := map[string][]byte{
"key1": []byte("value1"),
"key2": []byte("value2"),
}
cacher.SetMulti(multi, 0)
keys := []string{"key1", "key2"}
values, tokens, bools := cacher.GetMulti(keys)
fmt.Println(values["key1"], values["key2"])
fmt.Println(tokens["key1"], tokens["key2"])
fmt.Println(bools["key1"], bools["key2"])
// Output:
// [118 97 108 117 101 49] [118 97 108 117 101 50]
// 9946687e5fa0dab5993ededddb398d2e f066ce9385512ee02afc6e14d627e9f2
// <nil> <nil>
}
func ExampleIncrement() {
cacher.Flush()
cacher.Increment("key1", 0, 1, 0)
cacher.Increment("key1", 0, 1, 0)
v, _, _ := cacher.Get("key1")
num, _ := encoding.BytesInt64(v)
fmt.Println(num)
cacher.Set("key2", []byte("string value, not incrementable"), 0)
ok := cacher.Increment("key2", 0, 5, 0)
v2, _, _ := cacher.Get("key2")
fmt.Println(ok, string(v2))
// Output:
// 1
// Value for key `key2` could not be encoded. string value, not incrementable
}
func ExampleDecrement() {
cacher.Flush()
cacher.Decrement("key1", 10, 1, 0)
cacher.Decrement("key1", 10, 3, 0)
v, _, _ := cacher.Get("key1")
num, _ := encoding.BytesInt64(v)
fmt.Println(num)
cacher.Set("key2", []byte("string value, not decrementable"), 0)
ok := cacher.Decrement("key2", 0, 5, 0)
v2, _, _ := cacher.Get("key2")
fmt.Println(ok, string(v2))
// Output:
// 7
// Value for key `key2` could not be encoded. string value, not decrementable
}
func ExampleFlush() {
cacher.Flush()
var errs map[string]error
keys := []string{"key1", "key2"}
multi := map[string][]byte{
"key1": []byte("value1"),
"key2": []byte("value2"),
}
cacher.SetMulti(multi, 0)
_, _, errs = cacher.GetMulti(keys)
fmt.Println(errs["key1"], errs["key2"])
cacher.Flush()
_, _, errs = cacher.GetMulti(keys)
fmt.Println(errs["key1"], errs["key2"])
// Output:
// <nil> <nil>
// Key `key1` does not exist. Key `key2` does not exist.
}
func ExampleDelete() {
cacher.Flush()
var err error
cacher.Set("key1", []byte("value1"), 0)
err = cacher.Delete("key1")
fmt.Println(err)
err = cacher.Delete("non-existing")
fmt.Println(err)
// Output:
// <nil>
// Key `non-existing` was not found.
}
func ExampleDeleteMulti() {
cacher.Flush()
keys := []string{"key1", "key2", "non-existing"}
multi := map[string][]byte{
"key1": []byte("value1"),
"key2": []byte("value2"),
}
cacher.SetMulti(multi, 0)
oks := cacher.DeleteMulti(keys)
fmt.Println(oks["key1"])
fmt.Println(oks["key2"])
fmt.Println(oks["non-existing"])
// Output:
// <nil>
// <nil>
// Key `non-existing` was not found.
}
func ExampleTouch() {
cacher.Flush()
cacher.Set("key1", []byte("value1"), 1)
err := cacher.Touch("key1", 5)
fmt.Println(err)
// Output:
// <nil>
}