-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathrediscache.go
More file actions
201 lines (165 loc) · 4.94 KB
/
rediscache.go
File metadata and controls
201 lines (165 loc) · 4.94 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
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
package rediscache
import (
"bytes"
"context"
"encoding/gob"
"errors"
"github.com/go-redis/redis/v7"
"go.mercari.io/datastore"
"go.mercari.io/datastore/dsmiddleware/storagecache"
"time"
)
var _ storagecache.Storage = &cacheHandler{}
var _ datastore.Middleware = &cacheHandler{}
const defaultExpiration = 15 * time.Minute
// New Redis cache middleware creates & returns.
func New(conn *redis.Client, opts ...CacheOption) interface {
datastore.Middleware
storagecache.Storage
} {
// I want to make ch.dsmiddleware accessible from the test
ch := &cacheHandler{
conn: conn,
stOpts: &storagecache.Options{},
expireDuration: defaultExpiration,
}
for _, opt := range opts {
opt.Apply(ch)
}
s := storagecache.New(ch, ch.stOpts)
ch.Middleware = s
if ch.logf == nil {
ch.logf = func(ctx context.Context, format string, args ...interface{}) {}
}
if ch.cacheKey == nil {
ch.cacheKey = func(key datastore.Key) string {
return "mercari:rediscache:" + key.Encode()
}
}
return ch
}
type cacheHandler struct {
datastore.Middleware
stOpts *storagecache.Options
conn *redis.Client
expireDuration time.Duration
logf func(ctx context.Context, format string, args ...interface{})
cacheKey func(key datastore.Key) string
}
// A CacheOption is an cache option for a Redis cache middleware.
type CacheOption interface {
Apply(*cacheHandler)
}
func (ch *cacheHandler) SetMulti(ctx context.Context, cis []*storagecache.CacheItem) error {
ch.logf(ctx, "dsmiddleware/rediscache.SetMulti: incoming len=%d", len(cis))
pipe := ch.conn.TxPipeline()
cnt := 0
for _, ci := range cis {
if ci.Key.Incomplete() {
panic("incomplete key incoming")
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(ci.PropertyList)
if err != nil {
ch.logf(ctx, "dsmiddleware/rediscache.SetMulti: gob.Encode error key=%s err=%s", ci.Key.String(), err.Error())
continue
}
cacheKey := ch.cacheKey(ci.Key)
cacheValue := buf.Bytes()
if ch.expireDuration <= 0 {
err = pipe.Set(cacheKey, cacheValue, 0).Err()
if err != nil {
ch.logf(ctx, `dsmiddleware/rediscache.SetMulti: conn.Send("SET", "%s", ...) err=%s`, cacheKey, err.Error())
return err
}
} else {
err = ch.conn.Set(cacheKey, cacheValue, ch.expireDuration).Err()
if err != nil {
ch.logf(ctx, `dsmiddleware/rediscache.SetMulti: conn.Send("SET", "%s", ..., "PX", %d) err=%s`, cacheKey, ch.expireDuration/time.Millisecond, err.Error())
return err
}
}
cnt++
}
ch.logf(ctx, "dsmiddleware/rediscache.SetMulti: len=%d", cnt)
_, err := pipe.Exec()
if err != nil {
ch.logf(ctx, `dsmiddleware/rediscache.SetMulti: conn.Send("EXEC") err=%s`, err.Error())
return err
}
return nil
}
func (ch *cacheHandler) GetMulti(ctx context.Context, keys []datastore.Key) ([]*storagecache.CacheItem, error) {
ch.logf(ctx, "dsmiddleware/rediscache.GetMulti: incoming len=%d", len(keys))
pipe := ch.conn.TxPipeline()
resultList := make([]*storagecache.CacheItem, len(keys))
resps := make([]*redis.StringCmd, len(keys))
for idx, key := range keys {
cacheKey := ch.cacheKey(key)
resultList[idx] = &storagecache.CacheItem{
Key: key,
}
resps[idx] = pipe.Get(cacheKey)
}
_, err := pipe.Exec()
if err != nil && err != redis.Nil {
ch.logf(ctx, `dsmiddleware/rediscache.GetMulti: conn.Do("EXEC") err=%s`, err.Error())
return nil, err
}
hit := 0
miss := 0
for idx, r := range resps {
b, err := r.Bytes()
if err == redis.Nil {
resultList[idx] = nil
miss++
continue
}
if err != nil {
ch.logf(ctx, `dsmiddleware/rediscache.GetMulti: conn.Send("GET") err=%s`, err.Error())
return nil, err
}
if len(b) == 0 {
resultList[idx] = nil
miss++
continue
}
buf := bytes.NewBuffer(b)
dec := gob.NewDecoder(buf)
var ps datastore.PropertyList
err = dec.Decode(&ps)
if err != nil {
resultList[idx] = nil
ch.logf(ctx, "dsmiddleware/rediscache.GetMulti: gob.Decode error key=%s err=%s", keys[idx].String(), err.Error())
miss++
continue
}
if !resultList[idx].Key.Equal(keys[idx]) {
ch.logf(ctx, "dsmiddleware/rediscache.GetMulti: key equality check failed")
return nil, errors.New("dsmiddleware/rediscache.GetMulti: key equality check failed")
}
resultList[idx].PropertyList = ps
hit++
}
ch.logf(ctx, "dsmiddleware/rediscache.GetMulti: hit=%d miss=%d", hit, miss)
return resultList, nil
}
func (ch *cacheHandler) DeleteMulti(ctx context.Context, keys []datastore.Key) error {
ch.logf(ctx, "dsmiddleware/rediscache.DeleteMulti: incoming len=%d", len(keys))
pipe := ch.conn.TxPipeline()
for _, key := range keys {
cacheKey := ch.cacheKey(key)
err := pipe.Del(cacheKey).Err()
if err != nil {
ch.logf(ctx, `dsmiddleware/rediscache.DeleteMulti: conn.Send("DEL", "%s") err=%s`, cacheKey, err.Error())
return err
}
}
_, err := pipe.Exec()
if err != nil {
ch.logf(ctx, `dsmiddleware/rediscache.DeleteMulti: conn.Send("EXEC") err=%s`, err.Error())
return err
}
return nil
}