/
rcache.go
185 lines (157 loc) · 4.52 KB
/
rcache.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
package miso
import (
"errors"
"fmt"
"time"
"github.com/go-redis/redis"
)
const (
rcacheScanLimit int64 = 100
)
// Configuration of RCache.
type RCacheConfig struct {
//expire time for each entry
Exp time.Duration
// Disable use of distributed lock to synchronize access to the key in the cache.
//
// Most of the operations are atomic except Get(...) with supplier callback.
// If your are loading the cache manually using Put(...), then you probably don't need synchronization at all.
NoSync bool
}
// Redis Cache implementation.
//
// RCache internal isn't backed by an actual redis HSet. Cache name is simply the prefix for each key,
// and each key is stored independently.
//
// Use NewRCache(...) to instantiate.
type RCache[T any] struct {
ValueSerializer Serializer // serializer / deserializer
getClient Supplier[*redis.Client] // supplier of client (using func to make it lazy)
exp time.Duration // ttl for each cache entry
name string // name of the cache
sync bool // synchronize operation
}
func (r *RCache[T]) Put(rail Rail, key string, t T) error {
cacheKey := r.cacheKey(key)
val, err := r.ValueSerializer.Serialize(t)
if err != nil {
return fmt.Errorf("failed to serialze value, %w", err)
}
op := func() error {
return r.getClient().Set(cacheKey, val, r.exp).Err()
}
if r.sync {
return RLockExec(rail, r.lockKey(key), op)
}
return op()
}
func (r *RCache[T]) Del(rail Rail, key string) error {
cacheKey := r.cacheKey(key)
op := func() error {
return r.getClient().Del(cacheKey).Err()
}
if r.sync {
return RLockExec(rail, r.lockKey(key), op)
}
return op()
}
func (r *RCache[T]) cacheKey(key string) string {
return "rcache:" + r.name + ":" + key
}
func (r *RCache[T]) cacheKeyPattern() string {
return "rcache:" + r.name + ":*"
}
func (r *RCache[T]) lockKey(key string) string {
return "lock:" + r.cacheKey(key)
}
// Get from cache else run supplier
//
// Return miso.NoneErr if none is found
func (r *RCache[T]) Get(rail Rail, key string, supplier func() (T, error)) (T, error) {
// the actual operation
op := func() (T, error) {
cacheKey := r.cacheKey(key)
var t T
cmd := r.getClient().Get(cacheKey)
if cmd.Err() == nil {
return t, r.ValueSerializer.Deserialize(&t, cmd.Val()) // key found
}
if cmd.Err() != nil && !errors.Is(cmd.Err(), redis.Nil) { // cmd failed
return t, fmt.Errorf("failed to get value from redis, unknown error, %w", cmd.Err())
}
// nothing to supply, give up
if supplier == nil {
return t, NoneErr
}
// call supplier and cache the supplied value
supplied, err := supplier()
if err != nil {
return t, err
}
// serialize supplied value
v, err := r.ValueSerializer.Serialize(supplied)
if err != nil {
return t, fmt.Errorf("failed to serialize the supplied value, %w", err)
}
// cache the serialized value
scmd := r.getClient().Set(cacheKey, v, r.exp)
if scmd.Err() != nil {
return t, scmd.Err()
}
return supplied, nil
}
if r.sync {
return RLockRun(rail, r.lockKey(key), op)
}
return op()
}
func (r *RCache[T]) Exists(rail Rail, key string) (bool, error) {
op := func() (bool, error) {
cacheKey := r.cacheKey(key)
cmd := r.getClient().Exists(cacheKey)
if cmd.Err() == nil {
return cmd.Val() > 0, nil
}
if cmd.Err() != nil && !errors.Is(cmd.Err(), redis.Nil) { // cmd failed
return false, fmt.Errorf("failed to get value from redis, unknown error, %w", cmd.Err())
}
return false, nil
}
if r.sync {
return RLockRun(rail, r.lockKey(key), op)
}
return op()
}
func (r *RCache[T]) DelAll(rail Rail) error {
pat := r.cacheKeyPattern()
cmd := r.getClient().Scan(0, pat, rcacheScanLimit)
if cmd.Err() != nil {
return fmt.Errorf("failed to scan redis with pattern '%v', %w", pat, cmd.Err())
}
iter := cmd.Iterator()
for iter.Next() {
if iter.Err() != nil {
return fmt.Errorf("failed to iterate using scan, pattern: '%v', %w", pat, iter.Err())
}
key := iter.Val()
dcmd := r.getClient().Del(key)
if dcmd.Err() != nil {
if !errors.Is(dcmd.Err(), redis.Nil) {
return fmt.Errorf("failed to del key %v, %w", key, dcmd.Err())
}
} else {
rail.Debugf("Deleted rcache key %v", key)
}
}
return nil
}
// Create new RCache
func NewRCache[T any](name string, conf RCacheConfig) RCache[T] {
return RCache[T]{
getClient: func() *redis.Client { return GetRedis() },
exp: conf.Exp,
name: name,
sync: !conf.NoSync,
ValueSerializer: JsonSerializer{},
}
}