forked from VictoriaMetrics/fastcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_example_test.go
More file actions
389 lines (309 loc) · 8.42 KB
/
cache_example_test.go
File metadata and controls
389 lines (309 loc) · 8.42 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package fastcache_test
import (
"fmt"
"sort"
"go.dw1.io/fastcache"
)
// ExampleCache demonstrates basic cache operations.
func ExampleCache() {
// Create a new cache with capacity for 100 entries
cache := fastcache.New[string, string](100)
defer cache.Reset()
// Set a key-value pair
cache.Set("key1", "value1")
// Get the value
if value, ok := cache.Get("key1"); ok {
fmt.Println("Found:", value)
}
// Check if a key exists
if cache.Has("key1") {
fmt.Println("Key exists")
}
// Output:
// Found: value1
// Key exists
}
// ExampleCache_GetOrSet demonstrates the GetOrSet method.
func ExampleCache_GetOrSet() {
cache := fastcache.New[string, int](10)
defer cache.Reset()
// Key doesn't exist, so it will be set
value, loaded := cache.GetOrSet("counter", 1)
fmt.Printf("First call - Value: %d, Loaded: %t\n", value, loaded)
// Key exists, so existing value is returned
value, loaded = cache.GetOrSet("counter", 2)
fmt.Printf("Second call - Value: %d, Loaded: %t\n", value, loaded)
// Output:
// First call - Value: 1, Loaded: false
// Second call - Value: 1, Loaded: true
}
// ExampleCache_SetIfAbsent demonstrates the SetIfAbsent method.
func ExampleCache_SetIfAbsent() {
cache := fastcache.New[string, string](10)
defer cache.Reset()
// Key doesn't exist, so it will be set
stored := cache.SetIfAbsent("key", "first")
fmt.Printf("First set - Stored: %t\n", stored)
// Key exists, so it won't be set
stored = cache.SetIfAbsent("key", "second")
fmt.Printf("Second set - Stored: %t\n", stored)
// Check the value
if value, ok := cache.Get("key"); ok {
fmt.Println("Final value:", value)
}
// Output:
// First set - Stored: true
// Second set - Stored: false
// Final value: first
}
// ExampleCache_GetAndDelete demonstrates the GetAndDelete method.
func ExampleCache_GetAndDelete() {
cache := fastcache.New[string, string](10)
defer cache.Reset()
cache.Set("temp", "data")
// Get and delete the value
value, loaded := cache.GetAndDelete("temp")
fmt.Printf("Deleted value: %s, Was loaded: %t\n", value, loaded)
// Try to get it again - should not exist
if _, ok := cache.Get("temp"); !ok {
fmt.Println("Key no longer exists")
}
// Output:
// Deleted value: data, Was loaded: true
// Key no longer exists
}
// ExampleCache_All demonstrates iterating over all key-value pairs.
func ExampleCache_All() {
cache := fastcache.New[string, int](10)
defer cache.Reset()
// Add some data
cache.Set("item1", 42)
cache.Set("item2", 84)
fmt.Println("Cache entries:")
entries := make([]string, 0, 2)
for key, value := range cache.All() {
entries = append(entries, fmt.Sprintf("%s:%d", key, value))
}
sort.Strings(entries)
for _, entry := range entries {
fmt.Println(entry)
}
// Output:
// Cache entries:
// item1:42
// item2:84
}
// ExampleCache_Keys demonstrates iterating over all keys.
func ExampleCache_Keys() {
cache := fastcache.New[string, int](10)
defer cache.Reset()
cache.Set("x", 10)
cache.Set("y", 20)
cache.Set("z", 30)
fmt.Println("Keys found:")
keys := make([]string, 0, 3)
for key := range cache.Keys() {
keys = append(keys, key)
}
// Sort for consistent output
sort.Strings(keys)
for _, key := range keys {
fmt.Println(key)
}
// Output:
// Keys found:
// x
// y
// z
}
// ExampleCache_Values demonstrates iterating over all values.
func ExampleCache_Values() {
cache := fastcache.New[string, int](10)
defer cache.Reset()
cache.Set("p", 100)
cache.Set("q", 200)
cache.Set("r", 300)
fmt.Println("Values found:")
values := make([]int, 0, 3)
for value := range cache.Values() {
values = append(values, value)
}
// Sort for consistent output
sort.Ints(values)
for _, value := range values {
fmt.Println(value)
}
// Output:
// Values found:
// 100
// 200
// 300
}
// ExampleCache_Len demonstrates getting the cache size.
func ExampleCache_Len() {
cache := fastcache.New[string, string](10)
defer cache.Reset()
fmt.Println("Initial length:", cache.Len())
cache.Set("key1", "value1")
cache.Set("key2", "value2")
fmt.Println("After adding items:", cache.Len())
// Output:
// Initial length: 0
// After adding items: 2
}
// ExampleCache_Delete demonstrates deleting items from the cache.
func ExampleCache_Delete() {
cache := fastcache.New[string, string](10)
defer cache.Reset()
// Add some items
cache.Set("key1", "value1")
cache.Set("key2", "value2")
fmt.Println("Before deletion:")
fmt.Printf("Length: %d\n", cache.Len())
if _, ok := cache.Get("key1"); ok {
fmt.Println("key1 exists")
}
// Delete an item
cache.Delete("key1")
fmt.Println("After deletion:")
fmt.Printf("Length: %d\n", cache.Len())
if _, ok := cache.Get("key1"); !ok {
fmt.Println("key1 no longer exists")
}
// Output:
// Before deletion:
// Length: 2
// key1 exists
// After deletion:
// Length: 1
// key1 no longer exists
}
// ExampleCache_Reset demonstrates resetting the cache.
func ExampleCache_Reset() {
cache := fastcache.New[string, int](10)
// Add some items
cache.Set("a", 1)
cache.Set("b", 2)
cache.Set("c", 3)
fmt.Printf("Before reset - Length: %d\n", cache.Len())
// Reset the cache
cache.Reset()
fmt.Printf("After reset - Length: %d\n", cache.Len())
// Verify items are gone
if _, ok := cache.Get("a"); !ok {
fmt.Println("Cache is empty after reset")
}
// Output:
// Before reset - Length: 3
// After reset - Length: 0
// Cache is empty after reset
}
// ExampleNew demonstrates creating a new cache.
func ExampleNew() {
// Create a new cache with capacity for 100 entries
cache := fastcache.New[string, int](100)
defer cache.Reset()
fmt.Printf("Created cache with capacity: %d\n", 100)
fmt.Printf("Initial length: %d\n", cache.Len())
// Output:
// Created cache with capacity: 100
// Initial length: 0
}
// ExampleCache_Get demonstrates getting values from the cache.
func ExampleCache_Get() {
cache := fastcache.New[string, string](10)
defer cache.Reset()
// Set some values
cache.Set("name", "Alice")
cache.Set("age", "30")
// Get existing value
if value, ok := cache.Get("name"); ok {
fmt.Printf("Name: %s\n", value)
}
// Try to get non-existent value
if _, ok := cache.Get("city"); !ok {
fmt.Println("City not found")
}
// Output:
// Name: Alice
// City not found
}
// ExampleCache_Has demonstrates checking if keys exist in the cache.
func ExampleCache_Has() {
cache := fastcache.New[string, string](10)
defer cache.Reset()
cache.Set("user", "john")
// Check existing key
if cache.Has("user") {
fmt.Println("User exists")
}
// Check non-existent key
if !cache.Has("admin") {
fmt.Println("Admin does not exist")
}
// Output:
// User exists
// Admin does not exist
}
// ExampleCache_Set demonstrates setting values in the cache.
func ExampleCache_Set() {
cache := fastcache.New[string, int](10)
defer cache.Reset()
// Set some values
cache.Set("score", 100)
cache.Set("level", 5)
fmt.Printf("Cache length: %d\n", cache.Len())
// Values can be overwritten
cache.Set("score", 150)
if value, ok := cache.Get("score"); ok {
fmt.Printf("Updated score: %d\n", value)
}
// Output:
// Cache length: 2
// Updated score: 150
}
// ExampleStats_Reset demonstrates resetting stats.
func ExampleStats_Reset() {
var stats fastcache.Stats
// Simulate some stats
stats.GetCalls = 10
stats.SetCalls = 5
stats.Misses = 3
stats.Hits = 7
fmt.Printf("Before reset - GetCalls: %d, SetCalls: %d\n", stats.GetCalls, stats.SetCalls)
// Reset the stats
stats.Reset()
fmt.Printf("After reset - GetCalls: %d, SetCalls: %d\n", stats.GetCalls, stats.SetCalls)
// Output:
// Before reset - GetCalls: 10, SetCalls: 5
// After reset - GetCalls: 0, SetCalls: 0
}
// ExampleCache_UpdateStats demonstrates getting cache statistics.
func ExampleCache_UpdateStats() {
cache := fastcache.New[string, string](10)
defer cache.Reset()
// Perform some cache operations
cache.Set("key1", "value1")
cache.Set("key2", "value2")
cache.Get("key1") // This will be a hit
cache.Get("key3") // This will be a miss
cache.Delete("key2")
// Get statistics
var stats fastcache.Stats
cache.UpdateStats(&stats)
fmt.Printf("Get calls: %d\n", stats.GetCalls)
fmt.Printf("Set calls: %d\n", stats.SetCalls)
fmt.Printf("Hits: %d\n", stats.Hits)
fmt.Printf("Misses: %d\n", stats.Misses)
fmt.Printf("Deletes: %d\n", stats.Deletes)
fmt.Printf("Current entries: %d\n", stats.EntriesCount)
fmt.Printf("Max entries: %d\n", stats.MaxEntries)
// Output:
// Get calls: 2
// Set calls: 2
// Hits: 1
// Misses: 1
// Deletes: 1
// Current entries: 1
// Max entries: 10
}