-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompose.go
219 lines (189 loc) · 4.64 KB
/
compose.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
package cachestore
import (
"context"
"fmt"
"time"
)
type ComposeStore[V any] struct {
stores []Store[V]
}
func Compose[V any](stores ...Store[V]) (Store[V], error) {
if len(stores) == 0 {
return nil, fmt.Errorf("cachestore: attempting to compose with empty store list")
}
cs := &ComposeStore[V]{
stores: stores,
}
return cs, nil
}
func (cs *ComposeStore[V]) Exists(ctx context.Context, key string) (bool, error) {
for _, s := range cs.stores {
exists, err := s.Exists(ctx, key)
if err != nil {
return false, err
}
if exists {
return exists, nil
}
}
return false, nil
}
func (cs *ComposeStore[V]) Set(ctx context.Context, key string, value V) error {
for _, s := range cs.stores {
err := s.Set(ctx, key, value)
if err != nil {
return err
}
}
return nil
}
func (cs *ComposeStore[V]) SetEx(ctx context.Context, key string, value V, ttl time.Duration) error {
for _, s := range cs.stores {
err := s.SetEx(ctx, key, value, ttl)
if err != nil {
return err
}
}
return nil
}
func (cs *ComposeStore[V]) BatchSet(ctx context.Context, keys []string, values []V) error {
for _, s := range cs.stores {
err := s.BatchSet(ctx, keys, values)
if err != nil {
return err
}
}
return nil
}
func (cs *ComposeStore[V]) BatchSetEx(ctx context.Context, keys []string, values []V, ttl time.Duration) error {
for _, s := range cs.stores {
err := s.BatchSetEx(ctx, keys, values, ttl)
if err != nil {
return err
}
}
return nil
}
func (cs *ComposeStore[V]) GetEx(ctx context.Context, key string) (V, *time.Duration, bool, error) {
var out V
var ttl *time.Duration
var exists bool
var err error
for _, s := range cs.stores {
out, ttl, exists, err = s.GetEx(ctx, key)
if err != nil {
return out, ttl, exists, err
}
if exists {
break
}
}
return out, ttl, exists, nil
}
func (cs *ComposeStore[V]) Get(ctx context.Context, key string) (V, bool, error) {
var out V
var exists bool
var err error
for _, s := range cs.stores {
out, exists, err = s.Get(ctx, key)
if err != nil {
return out, exists, err
}
if exists {
return out, exists, nil
}
}
return out, exists, err
}
func (cs *ComposeStore[V]) BatchGet(ctx context.Context, keys []string) ([]V, []bool, error) {
fout := make([]V, len(keys))
fexists := make([]bool, len(keys))
// TODO: in the future, we can actually call BatchSet() on difference stores
// which are missing the keys, upward on the chain.. maybe downward too,
// but we'd probably need ComposeOptions in the constructor
fn := func(store Store[V], ctx context.Context) (bool, error) {
idx := make([]int, 0, len(keys))
k := make([]string, 0, len(keys))
for i, e := range fexists {
if e == false {
k = append(k, keys[i])
idx = append(idx, i)
}
}
if len(idx) == 0 {
// nothing to do, we done..
return true, nil
}
out, exists, err := store.BatchGet(ctx, k)
if err != nil {
return false, err
}
// todo, check all exists, etc..
for i, e := range exists {
if e {
fout[idx[i]] = out[i]
fexists[idx[i]] = true
}
}
done := true
for _, e := range fexists {
if !e {
done = false
break
}
}
return done, nil
}
for _, s := range cs.stores {
done, err := fn(s, ctx)
if err != nil {
return fout, fexists, err
}
if done {
break
}
}
return fout, fexists, nil
}
func (cs *ComposeStore[V]) Delete(ctx context.Context, key string) error {
for _, s := range cs.stores {
err := s.Delete(ctx, key)
if err != nil {
return err
}
}
return nil
}
func (cs *ComposeStore[V]) DeletePrefix(ctx context.Context, keyPrefix string) error {
if len(keyPrefix) < 4 {
return fmt.Errorf("cachestore: DeletePrefix keyPrefix '%s' must be at least 4 characters long", keyPrefix)
}
for _, s := range cs.stores {
err := s.DeletePrefix(ctx, keyPrefix)
if err != nil {
return err
}
}
return nil
}
func (cs *ComposeStore[V]) ClearAll(ctx context.Context) error {
for _, s := range cs.stores {
err := s.ClearAll(ctx)
if err != nil {
return err
}
}
return nil
}
func (cs *ComposeStore[V]) GetOrSetWithLock(
ctx context.Context, key string, getter func(context.Context, string) (V, error),
) (V, error) {
// Skip all intermediate stores and use only the last one as usually it's the most reliable one
return cs.stores[len(cs.stores)-1].GetOrSetWithLock(ctx, key, getter)
}
func (cs *ComposeStore[V]) GetOrSetWithLockEx(
ctx context.Context, key string, getter func(context.Context, string) (V, error), ttl time.Duration,
) (V, error) {
// Skip all intermediate stores and use only the last one as usually it's the most reliable one
return cs.stores[len(cs.stores)-1].GetOrSetWithLockEx(ctx, key, getter, ttl)
}