-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_example_test.go
104 lines (90 loc) · 2.39 KB
/
cache_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
// Copyright (c) 2022 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package cache_test
import (
"fmt"
"sync"
"time"
"github.com/tunabay/go-cache"
)
func Example_simple() {
// function to create a resource
createResource := func(key uint8) (string, time.Time, error) {
time.Sleep(time.Second >> 2) // slow creation
return fmt.Sprintf("resource %d", key), time.Time{}, nil
}
// cache with uint8 key and string value
myCache := cache.New[uint8, string](createResource)
var wg sync.WaitGroup
for i := 0; i < 12; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
key := uint8(n & 0b11)
val, cached, _, err := myCache.Get(key)
if err != nil {
panic(err)
}
var tag string
if cached {
tag = " (cached)"
}
fmt.Printf("%d -> %q%s\n", key, val, tag)
}(i)
}
wg.Wait()
// Unordered output:
// 0 -> "resource 0"
// 1 -> "resource 1"
// 2 -> "resource 2"
// 3 -> "resource 3"
// 0 -> "resource 0" (cached)
// 1 -> "resource 1" (cached)
// 2 -> "resource 2" (cached)
// 3 -> "resource 3" (cached)
// 0 -> "resource 0" (cached)
// 1 -> "resource 1" (cached)
// 2 -> "resource 2" (cached)
// 3 -> "resource 3" (cached)
}
func Example_struct() {
type myResource struct{ data string }
createResource := func(key string) (*myResource, time.Time, error) {
time.Sleep(time.Second >> 2) // slow creation
return &myResource{data: "resource " + key}, time.Time{}, nil
}
// cache with string key and *myResource value
myCache := cache.New[string, *myResource](createResource)
var wg sync.WaitGroup
for i := 0; i < 12; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
key := fmt.Sprintf("key-%d", n&0b11)
val, cached, _, err := myCache.Get(key)
if err != nil {
panic(err)
}
var tag string
if cached {
tag = " (cached)"
}
fmt.Printf("%s -> %q%s\n", key, val.data, tag)
}(i)
}
wg.Wait()
// Unordered output:
// key-0 -> "resource key-0"
// key-1 -> "resource key-1"
// key-2 -> "resource key-2"
// key-3 -> "resource key-3"
// key-0 -> "resource key-0" (cached)
// key-1 -> "resource key-1" (cached)
// key-2 -> "resource key-2" (cached)
// key-3 -> "resource key-3" (cached)
// key-0 -> "resource key-0" (cached)
// key-1 -> "resource key-1" (cached)
// key-2 -> "resource key-2" (cached)
// key-3 -> "resource key-3" (cached)
}