-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard.go
236 lines (189 loc) · 4.82 KB
/
shard.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
package mcache
import (
"context"
"errors"
"time"
)
type CachePolicy[T any] interface {
Init(clock Clock, capacity int)
Set(ctx context.Context, key string, val interface{}, ttl time.Duration) error
Get(ctx context.Context, key string) (interface{}, error)
Exists(ctx context.Context, key string) bool
Remove(ctx context.Context, key string) bool
Evict(ctx context.Context, count int)
*T
}
type cacheHandler[T any, P CachePolicy[T]] struct {
cache
shards []P
}
func newCacheHandler[T any, P CachePolicy[T]](b builder[T, P]) Cache {
c := &cacheHandler[T, P]{}
c.cache = b.cache
c.shards = make([]P, c.shardCount)
for i := 0; i < c.shardCount; i++ {
var p = P(new(T))
p.Init(c.clock, c.shardCap)
c.shards[i] = p
}
return c
}
func (c cacheHandler[T, P]) Set(ctx context.Context, key string, value interface{}, opts ...Option) error {
o := c.getOption(opts...)
defer c.putOpt(o)
if c.redisCli.Client != nil {
err := c.redisCli.set(ctx, key, value, o)
if err != nil {
return err
}
}
return c.getShard(key).Set(ctx, key, value, o.TTL)
}
func (c cacheHandler[T, P]) MSet(ctx context.Context, keys []string, values []interface{}, opts ...Option) error {
if len(keys) != len(values) {
return KeyValueLenError
}
o := c.getOption(opts...)
defer c.putOpt(o)
if c.redisCli.Client != nil {
err := c.redisCli.mset(ctx, keys, values, o)
if err != nil {
return err
}
}
for i, k := range keys {
if err := c.getShard(k).Set(ctx, k, values[i], o.TTL); err != nil {
return err
}
}
return nil
}
func (c cacheHandler[T, P]) Get(ctx context.Context, key string, opts ...Option) (interface{}, error) {
o := c.getOption(opts...)
defer c.putOpt(o)
s := c.getShard(key)
val, err := s.Get(ctx, key)
if err == KeyNotFoundError {
if o.RealLoaderFunc == nil {
return nil, KeyNotFoundError
}
val, err := o.RealLoaderFunc(ctx, key)
if err != nil {
if !errors.Is(err, DefaultValueSetError) {
return nil, err
}
if errors.Is(err, DefaultValueSetError) {
o.TTL = time.Minute
}
if err := s.Set(ctx, key, val, o.TTL); err != nil {
return nil, err
}
if errors.Is(err, DefaultValueSetError) {
return val, err
}
}
}
return val, nil
}
func (c cacheHandler[T, P]) MGet(ctx context.Context, keys []string, opts ...Option) (map[string]interface{}, error) {
o := c.getOption(opts...)
defer c.putOpt(o)
res := make(map[string]interface{}, len(keys))
miss := make(map[string]P, len(keys))
for _, key := range keys {
s := c.getShard(key)
val, err := s.Get(ctx, key)
if err == nil {
res[key] = val
} else if err == KeyNotFoundError {
miss[key] = s
}
}
if len(miss) > 0 {
if o.RealMLoaderFunc == nil {
goto END
}
keys := make([]string, 0, len(miss))
for key := range miss {
keys = append(keys, key)
}
kvs, err := o.RealMLoaderFunc(ctx, keys)
if err != nil {
goto END
}
for key, val := range kvs {
s := miss[key]
err := s.Set(ctx, key, val, o.TTL)
if err != nil {
goto END
}
res[key] = val
}
}
END:
return res, nil
}
func (c cacheHandler[T, P]) Remove(ctx context.Context, key string) bool {
if c.redisCli.Client != nil {
err := c.redisCli.del(ctx, key)
if err != nil {
return false
}
}
return c.getShard(key).Remove(ctx, key)
}
func (c cacheHandler[T, P]) MRemove(ctx context.Context, keys []string) bool {
if c.redisCli.Client != nil {
err := c.redisCli.mdel(ctx, keys)
if err != nil {
return false
}
}
for _, key := range keys {
if !c.getShard(key).Remove(ctx, key) {
return false
}
}
return true
}
func (c cacheHandler[T, P]) Exists(ctx context.Context, key string) bool {
return c.getShard(key).Exists(ctx, key)
}
func (c cacheHandler[T, P]) getShard(key string) P {
return c.shards[c.DebugShardIndex(key)]
}
func (c cacheHandler[T, P]) DebugShardIndex(key string) uint64 {
return MemHashString(key) & uint64(c.shardCount-1)
}
func (c cacheHandler[T, P]) debugLocalGet(ctx context.Context, key string) (interface{}, error) {
val, err := c.getShard(key).Get(ctx, key)
if err != nil {
return nil, err
}
return val, nil
}
func (c cacheHandler[T, P]) debugLocalRemove(ctx context.Context, key string) bool {
return c.getShard(key).Remove(ctx, key)
}
func (c cacheHandler[T, P]) serialize(ctx context.Context, val interface{}, opts ...Option) ([]byte, error) {
o := c.getOption(opts...)
defer c.putOpt(o)
if o.serializeFunc != nil {
return o.serializeFunc(ctx, val)
}
if c.serializeFunc != nil {
return c.serializeFunc(ctx, val)
}
return nil, SerializeError
}
func (c cacheHandler[T, P]) deserialize(ctx context.Context, data []byte, opts ...Option) (interface{}, error) {
o := c.getOption(opts...)
defer c.putOpt(o)
if o.deserializeFunc != nil {
return o.deserializeFunc(ctx, data)
}
if c.deserializeFunc != nil {
return c.deserializeFunc(ctx, data)
}
return nil, SerializeError
}