-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayered.go
233 lines (207 loc) · 7.13 KB
/
layered.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
// Copyright 2024 geebytes. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package golayeredcache
import (
"context"
"fmt"
"time"
"github.com/begonia-org/go-layered-cache/gocuckoo"
"github.com/begonia-org/go-layered-cache/local"
"github.com/begonia-org/go-layered-cache/source"
"github.com/redis/go-redis/v9"
"github.com/sirupsen/logrus"
)
type CacheReadStrategy int
const (
// Read from the local cache only
LocalOnly CacheReadStrategy = iota
// Read from the local cache first, then from the source cache
LocalThenSource
)
type WithDelOperator interface {
Del(ctx context.Context, key interface{}, args ...interface{}) error
}
type Watcher interface {
Watch(ctx context.Context) <-chan error
UnWatch() error
}
type Loader interface {
LoadDump(ctx context.Context) error
}
// LayeredLocalCache is the interface for the local cache
type LayeredLocalCache interface{
GetFromLocal(ctx context.Context, key interface{}, args ...interface{}) ([]interface{}, error)
SetToLocal(ctx context.Context, key interface{}, args ...interface{}) error
}
// LayeredCache is the interface for the layered cache
type LayeredCache interface {
LayeredLocalCache
// Get key value
Get(ctx context.Context, key interface{}, args ...interface{}) ([]interface{}, error)
Set(ctx context.Context, key interface{}, args ...interface{}) error
Watch(ctx context.Context) <-chan error
UnWatch() error
// OnMessage is a callback function for the message from the source cache
OnMessage(ctx context.Context, from string, message interface{}) error
LoadDump(ctx context.Context) error
}
type LayeredKeyValueCache interface {
// LayeredCache
LayeredLocalCache
Watcher
Loader
Get(ctx context.Context, key string) ([]byte, error)
Set(ctx context.Context, key string, value []byte, exp time.Duration) error
Del(ctx context.Context, key string) error
}
type LayeredFilter interface {
Watcher
Loader
Check(ctx context.Context, key string, value []byte) (bool, error)
Add(ctx context.Context, key string, value []byte) error
AddLocalFilter(key string, filter local.Filter) error
}
type LayeredCuckooFilter interface {
LayeredFilter
Del(ctx context.Context, key string, value []byte) error
}
// LayeredBuildOptions is the options for building the layered cache
type LayeredBuildOptions struct {
// RDB is the redis client as the source cache
RDB *redis.Client
// Watcher is the watch options for the source cache
Watcher *source.WatchOptions
// Log is the logger
Log *logrus.Logger
// Channel is the channel for the source cache
// It is used to receive the message from the source cache
// It is also used to publish the message to the source cache
// In the case of the source cache is redis, it is the redis xstream channel
Channel interface{}
// KeyPrefix is the key prefix for cache,like "cache:test:bloom"
KeyPrefix string
// Strategy is the read strategy
// It is used to determine the read strategy when the cache is read.
// For Read from the local cache only, it is LocalOnly.
// For Read from the local cache first, then from the source cache when the local cache is none, it is LocalThenSource
Strategy CacheReadStrategy
}
// LayeredBloomFilterOptions is the options for building the layered bloom filter
type LayeredCuckooFilterOptions struct {
RDB *redis.Client
Watcher *source.WatchOptions
Log *logrus.Logger
Entries uint64
Errors float64
Channel interface{}
KeyPrefix string
Strategy CacheReadStrategy
DefaultBuildCuckooOptions *gocuckoo.CuckooBuildOptions
}
type BaseLayeredCacheImpl struct {
// 一级缓存
source source.DataSource
// 二级缓存
local local.LocalCache
log *logrus.Logger
strategy CacheReadStrategy
watchRunning bool
watchCancel context.CancelFunc
}
func (lc *BaseLayeredCacheImpl) Get(ctx context.Context, key interface{}, args ...interface{}) ([]interface{}, error) {
vals, err := lc.local.Get(ctx, key.(string), args...)
if err != nil || len(vals) == 0 {
if err != nil {
lc.log.Errorf("local.Get %v error:%v",key, err)
}
if lc.strategy == LocalOnly {
return nil, err
}
vals, err = lc.source.Get(ctx, key.(string), args...)
if err != nil {
return nil, err
}
if len(vals) > 0 {
err = lc.local.Set(ctx, key.(string), vals...)
if err != nil {
lc.log.Errorf("local.Set:%v", err)
}
}
return vals, err
}
return vals, err
}
func (lc *BaseLayeredCacheImpl) SetStrategy(strategy CacheReadStrategy) {
lc.strategy = strategy
}
func (lc *BaseLayeredCacheImpl) SetToLocal(ctx context.Context, key interface{}, args ...interface{}) error {
return lc.local.Set(ctx, key, args...)
}
func (lc *BaseLayeredCacheImpl) GetFromLocal(ctx context.Context, key interface{}, args ...interface{}) ([]interface{}, error) {
return lc.local.Get(ctx, key, args...)
}
func (lc *BaseLayeredCacheImpl) DelOnLocal(ctx context.Context, key interface{}, args ...interface{}) error {
if _, ok := lc.local.(WithDelOperator); !ok {
return fmt.Errorf("local cache not implement WithDelOperator")
}
return lc.local.(WithDelOperator).Del(ctx, key, args...)
}
func (lc *BaseLayeredCacheImpl) Set(ctx context.Context, key interface{}, args ...interface{}) error {
err := lc.local.Set(ctx, key.(string), args...)
if err != nil {
lc.log.Errorf("local.Set:%v", err)
return err
}
err = lc.source.Set(ctx, key.(string), args...)
if err != nil {
lc.log.Errorf("source.Set:%v", err)
return err
}
return err
}
func (lc *BaseLayeredCacheImpl) Del(ctx context.Context, key interface{}, args ...interface{}) error {
if _, ok := lc.local.(WithDelOperator); !ok {
return fmt.Errorf("local cache not implement WithDelOperator")
}
err := lc.local.(WithDelOperator).Del(ctx, key.(string), args...)
if err != nil {
lc.log.Error("local.Del", err)
}
err = lc.source.(WithDelOperator).Del(ctx, key.(string), args...)
return err
}
func (lc *BaseLayeredCacheImpl) Watch(ctx context.Context, onMessage source.OnMessage) <-chan error {
if lc.watchRunning {
lc.log.Warning("watch is running,if you want to watch again,please call UnWatch first")
return nil
}
ch, cancel := lc.source.Watch(ctx, onMessage)
lc.watchCancel = cancel
lc.watchRunning = true
return ch
}
func (lc *BaseLayeredCacheImpl) UnWatch() error {
if lc.watchRunning {
lc.watchCancel()
lc.watchRunning = false
}
return nil
}
func (lc *BaseLayeredCacheImpl) Dump(ctx context.Context, key interface{}, args ...interface{}) <-chan interface{} {
return lc.source.Dump(ctx, key.(string), args...)
}
func (lc *BaseLayeredCacheImpl) Load(ctx context.Context, key interface{}, args ...interface{}) error {
return lc.local.Load(ctx, key.(string), args...)
}
func (lc *BaseLayeredCacheImpl) Scan(ctx context.Context, pattern string, onScan source.OnScan) <-chan error {
return lc.source.Scan(ctx, pattern, onScan)
}
func newBaseLayeredCacheImpl(source source.DataSource, local local.LocalCache, log *logrus.Logger, strategy CacheReadStrategy) *BaseLayeredCacheImpl {
return &BaseLayeredCacheImpl{
source: source,
local: local,
log: log,
strategy: strategy,
}
}