-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
263 lines (240 loc) · 6.07 KB
/
cache.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
// Copyright 2022 tree xie
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cache
import (
"context"
"errors"
"time"
)
const (
// CompressNone compress none
CompressNone byte = iota
// Compressed compress
Compressed
)
type Cache struct {
keyPrefix string
ttlList []time.Duration
stores []Store
compressor Compressor
}
var ErrIsNil = errors.New("Data is nil")
var ErrKeyIsNil = errors.New("Key is nil")
// New creates a new cache with default ttl
func New(ttl time.Duration, opts ...CacheOption) (*Cache, error) {
opt := Option{}
for _, fn := range opts {
fn(&opt)
}
store := opt.store
// 如果未指定store,则使用big cache
if store == nil {
s, err := newBigCacheStore(ttl, &opt)
if err != nil {
return nil, err
}
store = s
}
stores := []Store{
store,
}
if opt.secondaryStore != nil {
stores = append(stores, opt.secondaryStore)
}
ttlList := opt.ttlList
if len(ttlList) == 0 {
ttlList = []time.Duration{
ttl,
}
}
return &Cache{
compressor: opt.compressor,
keyPrefix: opt.keyPrefix,
ttlList: ttlList,
stores: stores,
}, nil
}
// Close closes all stores of cache
func (c *Cache) Close(ctx context.Context) error {
for _, s := range c.stores {
err := s.Close(ctx)
if err != nil {
return err
}
}
return nil
}
func (c *Cache) getKey(key string) (string, error) {
if key == "" {
return "", ErrKeyIsNil
}
return c.keyPrefix + key, nil
}
func (c *Cache) getTTL(index int, ttl ...time.Duration) time.Duration {
if len(ttl) != 0 {
return ttl[0]
}
if len(c.ttlList) > index {
return c.ttlList[index]
}
return c.ttlList[0]
}
func (c *Cache) getBytes(ctx context.Context, key string) ([]byte, time.Duration, error) {
key, err := c.getKey(key)
if err != nil {
return nil, 0, err
}
max := len(c.stores)
var data []byte
var expiredAt time.Time
now := time.Now()
var ttl time.Duration
for index, s := range c.stores {
buf, err := s.Get(ctx, key)
// 出错,而且是最后一个store
// 则直接返回
if err != nil && index == max-1 {
return nil, 0, err
}
// 如果获取到数据
if len(buf) >= timestampByteSize {
expiredAt = getTimeFromBytes(buf)
// 如果已过期,继续查询
ttl = expiredAt.Sub(now)
if ttl < 0 {
continue
}
// 第一个store的数据已过期,将数据重新设置至store
// 一般情况下index为0,由于bigcache可能因为空间不足导致数据清除
// 或者二级缓存是redis,其它实例有操作更新
if index != 0 {
// 如果当前缓存对应的ttl
// 少于第一个缓存的ttl(内存缓存有效期有可能较短),则
// 使用新的ttl来修改记录
firstIndex := 0
newTTL := c.getTTL(firstIndex, ttl)
if newTTL < ttl {
ttl = newTTL
writeTimeToBytes(time.Now().Add(ttl), data)
}
// 设置失败则忽略
_ = c.stores[firstIndex].Set(ctx, key, buf, ttl)
}
data = buf[timestampByteSize:]
break
}
}
if len(data) == 0 {
return nil, 0, ErrIsNil
}
// 如果有配置压缩
if c.compressor != nil {
buf, err := c.compressor.Decode(data)
if err != nil {
return nil, 0, err
}
data = buf
}
return data, ttl, nil
}
// GetBytes gets the data from cache
func (c *Cache) GetBytes(ctx context.Context, key string) ([]byte, error) {
buf, _, err := c.getBytes(ctx, key)
return buf, err
}
// GetBytesAndTTL gets the data from cache and the ttl of data
func (c *Cache) GetBytesAndTTL(ctx context.Context, key string) ([]byte, time.Duration, error) {
return c.getBytes(ctx, key)
}
func (c *Cache) setBytes(ctx context.Context, key string, value []byte, ttls ...time.Duration) error {
key, err := c.getKey(key)
if err != nil {
return err
}
// 如果有设置解压
if c.compressor != nil {
buf, err := c.compressor.Encode(value)
if err != nil {
return err
}
value = buf
}
// 增加ttl至value中
data := make([]byte, len(value)+timestampByteSize)
copy(data[timestampByteSize:], value)
for index, s := range c.stores {
ttl := c.getTTL(index, ttls...)
writeTimeToBytes(time.Now().Add(ttl), data)
err := s.Set(ctx, key, data, ttl)
if err != nil {
return err
}
}
return nil
}
// SetBytes sets the data to cache
func (c *Cache) SetBytes(ctx context.Context, key string, value []byte, ttl ...time.Duration) error {
return c.setBytes(ctx, key, value, ttl...)
}
// Set marshals the value to bytes and sets to cache
func (c *Cache) Set(ctx context.Context, key string, value any, ttl ...time.Duration) error {
entry, err := marshal(value)
if err != nil {
return err
}
return c.setBytes(ctx, key, entry, ttl...)
}
func Get[T any](ctx context.Context, c *Cache, key string) (*T, error) {
v := new(T)
err := c.Get(ctx, key, v)
if err != nil {
return nil, err
}
return v, nil
}
// Get gets the value from cache and unmarshals it
func (c *Cache) Get(ctx context.Context, key string, value any) error {
data, _, err := c.getBytes(ctx, key)
if err != nil {
return err
}
return unmarshal(data, value)
}
// GetAndTTL gets the value from cache and unmarshals it, and returns the ttl of value
func (c *Cache) GetAndTTL(ctx context.Context, key string, value any) (time.Duration, error) {
data, ttl, err := c.getBytes(ctx, key)
if err != nil {
return 0, err
}
err = unmarshal(data, value)
if err != nil {
return 0, err
}
return ttl, nil
}
// Delete deletes all the data from all stores
func (c *Cache) Delete(ctx context.Context, key string) error {
key, err := c.getKey(key)
if err != nil {
return err
}
for _, s := range c.stores {
e := s.Delete(ctx, key)
// 无论是否出错均继续删除
if e != nil {
err = e
}
}
return err
}