-
Notifications
You must be signed in to change notification settings - Fork 33
/
level.go
275 lines (248 loc) · 5.83 KB
/
level.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package entcache
import (
"bytes"
"context"
"database/sql/driver"
"encoding/gob"
"errors"
"fmt"
"sync"
"time"
"github.com/golang/groupcache/lru"
"github.com/redis/go-redis/v9"
)
type (
// Entry defines an entry to store in a cache.
Entry struct {
Columns []string
Values [][]driver.Value
}
// A Key defines a comparable Go value.
// See http://golang.org/ref/spec#Comparison_operators
Key any
// AddGetDeleter defines the interface for getting,
// adding and deleting entries from the cache.
AddGetDeleter interface {
Del(context.Context, Key) error
Add(context.Context, Key, *Entry, time.Duration) error
Get(context.Context, Key) (*Entry, error)
}
)
func init() {
// Register non builtin driver.Values.
gob.Register(time.Time{})
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (e Entry) MarshalBinary() ([]byte, error) {
entry := struct {
C []string
V [][]driver.Value
}{
C: e.Columns,
V: e.Values,
}
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(entry); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (e *Entry) UnmarshalBinary(buf []byte) error {
var entry struct {
C []string
V [][]driver.Value
}
if err := gob.NewDecoder(bytes.NewBuffer(buf)).Decode(&entry); err != nil {
return err
}
e.Values = entry.V
e.Columns = entry.C
return nil
}
// ErrNotFound is returned by Get when and Entry does not exist in the cache.
var ErrNotFound = errors.New("entcache: entry was not found")
type (
// LRU provides an LRU cache that implements the AddGetter interface.
LRU struct {
mu sync.Mutex
*lru.Cache
}
// entry wraps the Entry with additional expiry information.
entry struct {
*Entry
expiry time.Time
}
)
// NewLRU creates a new Cache.
// If maxEntries is zero, the cache has no limit.
func NewLRU(maxEntries int) *LRU {
return &LRU{
Cache: lru.New(maxEntries),
}
}
// Add adds the entry to the cache.
func (l *LRU) Add(_ context.Context, k Key, e *Entry, ttl time.Duration) error {
l.mu.Lock()
defer l.mu.Unlock()
buf, err := e.MarshalBinary()
if err != nil {
return err
}
ne := &Entry{}
if err := ne.UnmarshalBinary(buf); err != nil {
return err
}
if ttl == 0 {
l.Cache.Add(k, ne)
} else {
l.Cache.Add(k, &entry{Entry: ne, expiry: time.Now().Add(ttl)})
}
return nil
}
// Get gets an entry from the cache.
func (l *LRU) Get(_ context.Context, k Key) (*Entry, error) {
l.mu.Lock()
e, ok := l.Cache.Get(k)
l.mu.Unlock()
if !ok {
return nil, ErrNotFound
}
switch e := e.(type) {
case *Entry:
return e, nil
case *entry:
if time.Now().Before(e.expiry) {
return e.Entry, nil
}
l.mu.Lock()
l.Cache.Remove(k)
l.mu.Unlock()
return nil, ErrNotFound
default:
return nil, fmt.Errorf("entcache: unexpected entry type: %T", e)
}
}
// Del deletes an entry from the cache.
func (l *LRU) Del(_ context.Context, k Key) error {
l.mu.Lock()
l.Cache.Remove(k)
l.mu.Unlock()
return nil
}
// Redis provides a remote cache backed by Redis
// and implements the SetGetter interface.
type Redis struct {
c redis.Cmdable
}
// NewRedis returns a new Redis cache level from the given Redis connection.
//
// entcache.NewRedis(redis.NewClient(&redis.Options{
// Addr: ":6379"
// }))
//
// entcache.NewRedis(redis.NewClusterClient(&redis.ClusterOptions{
// Addrs: []string{":7000", ":7001", ":7002"},
// }))
func NewRedis(c redis.Cmdable) *Redis {
return &Redis{c: c}
}
// Add adds the entry to the cache.
func (r *Redis) Add(ctx context.Context, k Key, e *Entry, ttl time.Duration) error {
key := fmt.Sprint(k)
if key == "" {
return nil
}
buf, err := e.MarshalBinary()
if err != nil {
return err
}
if err := r.c.Set(ctx, key, buf, ttl).Err(); err != nil {
return err
}
return nil
}
// Get gets an entry from the cache.
func (r *Redis) Get(ctx context.Context, k Key) (*Entry, error) {
key := fmt.Sprint(k)
if key == "" {
return nil, ErrNotFound
}
buf, err := r.c.Get(ctx, key).Bytes()
if err != nil || len(buf) == 0 {
return nil, ErrNotFound
}
e := &Entry{}
if err := e.UnmarshalBinary(buf); err != nil {
return nil, err
}
return e, nil
}
// Del deletes an entry from the cache.
func (r *Redis) Del(ctx context.Context, k Key) error {
key := fmt.Sprint(k)
if key == "" {
return nil
}
return r.c.Del(ctx, key).Err()
}
// multiLevel provides a multi-level cache implementation.
type multiLevel struct {
levels []AddGetDeleter
}
// Add adds the entry to the cache.
func (m *multiLevel) Add(ctx context.Context, k Key, e *Entry, ttl time.Duration) error {
for i := range m.levels {
if err := m.levels[i].Add(ctx, k, e, ttl); err != nil {
return err
}
}
return nil
}
// Get gets an entry from the cache.
func (m *multiLevel) Get(ctx context.Context, k Key) (*Entry, error) {
for i := range m.levels {
switch e, err := m.levels[i].Get(ctx, k); {
case err == nil:
return e, nil
case err != ErrNotFound:
return nil, err
}
}
return nil, ErrNotFound
}
// Del deletes an entry from the cache.
func (m *multiLevel) Del(ctx context.Context, k Key) error {
for i := range m.levels {
if err := m.levels[i].Del(ctx, k); err != nil {
return err
}
}
return nil
}
// contextLevel provides a context/request level cache implementation.
type contextLevel struct{}
// Get gets an entry from the cache.
func (*contextLevel) Get(ctx context.Context, k Key) (*Entry, error) {
c, ok := FromContext(ctx)
if !ok {
return nil, ErrNotFound
}
return c.Get(ctx, k)
}
// Add adds the entry to the cache.
func (*contextLevel) Add(ctx context.Context, k Key, e *Entry, ttl time.Duration) error {
c, ok := FromContext(ctx)
if !ok {
return nil
}
return c.Add(ctx, k, e, ttl)
}
// Del deletes an entry from the cache.
func (*contextLevel) Del(ctx context.Context, k Key) error {
c, ok := FromContext(ctx)
if !ok {
return nil
}
return c.Del(ctx, k)
}