-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache_util_test.go
More file actions
102 lines (79 loc) · 1.82 KB
/
cache_util_test.go
File metadata and controls
102 lines (79 loc) · 1.82 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
package crema
import (
"context"
"sync"
"time"
)
type testMemoryProvider[V any] struct {
mu sync.Mutex
items map[string]CacheObject[V]
}
func (m *testMemoryProvider[V]) Get(_ context.Context, key string) (CacheObject[V], bool, error) {
m.mu.Lock()
defer m.mu.Unlock()
value, ok := m.items[key]
return value, ok, nil
}
func (m *testMemoryProvider[V]) Set(_ context.Context, key string, value CacheObject[V], _ time.Duration) error {
m.mu.Lock()
defer m.mu.Unlock()
m.items[key] = value
return nil
}
func (m *testMemoryProvider[V]) Delete(_ context.Context, key string) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.items, key)
return nil
}
type byteProvider struct {
mu sync.Mutex
items map[string][]byte
}
func (b *byteProvider) Get(_ context.Context, key string) ([]byte, bool, error) {
b.mu.Lock()
defer b.mu.Unlock()
value, ok := b.items[key]
return value, ok, nil
}
func (b *byteProvider) Set(_ context.Context, key string, value []byte, _ time.Duration) error {
b.mu.Lock()
defer b.mu.Unlock()
b.items[key] = value
return nil
}
func (b *byteProvider) Delete(_ context.Context, key string) error {
b.mu.Lock()
defer b.mu.Unlock()
delete(b.items, key)
return nil
}
type errorProvider[S any] struct {
getErr error
setErr error
deleteErr error
}
func (p *errorProvider[S]) Get(_ context.Context, _ string) (S, bool, error) {
var zero S
if p.getErr != nil {
return zero, false, p.getErr
}
return zero, false, nil
}
func (p *errorProvider[S]) Set(_ context.Context, _ string, _ S, _ time.Duration) error {
if p.setErr != nil {
return p.setErr
}
return nil
}
func (p *errorProvider[S]) Delete(_ context.Context, _ string) error {
if p.deleteErr != nil {
return p.deleteErr
}
return nil
}
func fakeRandom(value float64) func() float64 {
return func() float64 {
return value
}
}