forked from C-Pro/geche
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummy_test.go
161 lines (120 loc) · 2.55 KB
/
dummy_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
package geche
// This file contains several simple map cache implementations for benchmark purposes.
// 1) Non generic version with hardcoded types.
// 2) Non thread-safe non generic version with hardcoded types.
// 3) interface{} based version.
import (
"sync"
)
type stringCache struct {
data map[string]string
mux sync.RWMutex
}
func newStringCache() *stringCache {
return &stringCache{
data: make(map[string]string),
}
}
func (s *stringCache) Set(key, value string) {
s.mux.Lock()
defer s.mux.Unlock()
s.data[key] = value
}
func (s *stringCache) SetIfPresent(key, value string) (string, bool) {
s.mux.Lock()
defer s.mux.Unlock()
old, ok := s.data[key]
if !ok {
return "", false
}
s.data[key] = value
return old, true
}
func (s *stringCache) Get(key string) (string, error) {
s.mux.RLock()
defer s.mux.RUnlock()
v, ok := s.data[key]
if !ok {
return v, ErrNotFound
}
return v, nil
}
func (s *stringCache) Del(key string) error {
s.mux.Lock()
defer s.mux.Unlock()
delete(s.data, key)
return nil
}
func (s *stringCache) Snapshot() map[string]string { return nil }
func (s *stringCache) Len() int {
s.mux.RLock()
defer s.mux.RUnlock()
return len(s.data)
}
type unsafeCache struct {
data map[string]string
}
func newUnsafeCache() *unsafeCache {
return &unsafeCache{
data: make(map[string]string),
}
}
func (u *unsafeCache) Set(key, value string) {
u.data[key] = value
}
func (u *unsafeCache) SetIfPresent(key, value string) (string, bool) {
old, err := u.Get(key)
if err != nil {
return "", false
}
u.Set(key, value)
return old, true
}
func (u *unsafeCache) Get(key string) (string, error) {
v, ok := u.data[key]
if !ok {
return v, ErrNotFound
}
return v, nil
}
func (u *unsafeCache) Del(key string) error {
delete(u.data, key)
return nil
}
func (u *unsafeCache) Snapshot() map[string]string { return nil }
func (u *unsafeCache) Len() int {
return len(u.data)
}
type anyCache struct {
data map[string]any
mux sync.RWMutex
}
func newAnyCache() *anyCache {
return &anyCache{
data: make(map[string]any),
}
}
func (a *anyCache) Set(key string, value any) {
a.mux.Lock()
defer a.mux.Unlock()
a.data[key] = value
}
func (a *anyCache) Get(key string) (any, error) {
a.mux.RLock()
defer a.mux.RUnlock()
v, ok := a.data[key]
if !ok {
return v, ErrNotFound
}
return v, nil
}
func (a *anyCache) Del(key string) error {
delete(a.data, key)
return nil
}
func (a *anyCache) Snapshot() map[string]any { return nil }
func (a *anyCache) Len() int {
a.mux.RLock()
defer a.mux.RUnlock()
return len(a.data)
}