-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathmemory.go
238 lines (203 loc) · 5.46 KB
/
memory.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
package cache
import (
"bytes"
"context"
"errors"
"fmt"
"reflect"
"sync"
"time"
"github.com/dgraph-io/ristretto"
"github.com/go-dev-frame/sponge/pkg/encoding"
)
type options struct {
numCounters int64
maxCost int64
bufferItems int64
}
func defaultOptions() *options {
return &options{
numCounters: 1e7, // number of keys to track frequency of (10M).
maxCost: 1 << 30, // maximum cost of cache (1GB).
bufferItems: 64, // number of keys per Get buffer.
}
}
// Option set the jwt options.
type Option func(*options)
func (o *options) apply(opts ...Option) {
for _, opt := range opts {
opt(o)
}
}
// WithNumCounters set number of keys.
func WithNumCounters(numCounters int64) Option {
return func(o *options) {
o.numCounters = numCounters
}
}
// WithMaxCost set maximum cost of cache.
func WithMaxCost(maxCost int64) Option {
return func(o *options) {
o.maxCost = maxCost
}
}
// WithBufferItems set number of keys per Get buffer.
func WithBufferItems(bufferItems int64) Option {
return func(o *options) {
o.bufferItems = bufferItems
}
}
// InitMemory create a memory cache
func InitMemory(opts ...Option) *ristretto.Cache {
o := defaultOptions()
o.apply(opts...)
// see: https://dgraph.io/blog/post/introducing-ristretto-high-perf-go-cache/
// https://www.start.io/blog/we-chose-ristretto-cache-for-go-heres-why/
config := &ristretto.Config{
NumCounters: o.numCounters,
MaxCost: o.maxCost,
BufferItems: o.bufferItems,
}
store, err := ristretto.NewCache(config)
if err != nil {
panic(err)
}
return store
}
// ----------------------------------------------------------------------------
// global memory cache client
var (
memoryCli *ristretto.Cache
once sync.Once
)
// InitGlobalMemory init global memory cache
func InitGlobalMemory(opts ...Option) {
memoryCli = InitMemory(opts...)
}
// GetGlobalMemoryCli get memory cache client
func GetGlobalMemoryCli() *ristretto.Cache {
if memoryCli == nil {
once.Do(func() {
memoryCli = InitMemory() // default options
})
}
return memoryCli
}
// CloseGlobalMemory close memory cache
func CloseGlobalMemory() error {
if memoryCli != nil {
memoryCli.Close()
}
return nil
}
// ----------------------------------------------------------------------------
type memoryCache struct {
client *ristretto.Cache
KeyPrefix string
encoding encoding.Encoding
DefaultExpireTime time.Duration
newObject func() interface{}
}
// NewMemoryCache create a memory cache
func NewMemoryCache(keyPrefix string, encode encoding.Encoding, newObject func() interface{}) Cache {
return &memoryCache{
client: GetGlobalMemoryCli(),
KeyPrefix: keyPrefix,
encoding: encode,
newObject: newObject,
}
}
// Set data
func (m *memoryCache) Set(_ context.Context, key string, val interface{}, expiration time.Duration) error {
buf, err := encoding.Marshal(m.encoding, val)
if err != nil {
return fmt.Errorf("encoding.Marshal error: %v, key=%s, val=%+v ", err, key, val)
}
if len(buf) == 0 {
buf = NotFoundPlaceholderBytes
}
cacheKey, err := BuildCacheKey(m.KeyPrefix, key)
if err != nil {
return fmt.Errorf("BuildCacheKey error: %v, key=%s", err, key)
}
ok := m.client.SetWithTTL(cacheKey, buf, 0, expiration)
if !ok {
return errors.New("SetWithTTL failed")
}
m.client.Wait()
return nil
}
// Get data
func (m *memoryCache) Get(_ context.Context, key string, val interface{}) error {
cacheKey, err := BuildCacheKey(m.KeyPrefix, key)
if err != nil {
return fmt.Errorf("BuildCacheKey error: %v, key=%s", err, key)
}
data, ok := m.client.Get(cacheKey)
if !ok {
return CacheNotFound // not found, convert to redis nil error
}
dataBytes, ok := data.([]byte)
if !ok {
return fmt.Errorf("data type error, key=%s, type=%T", key, data)
}
if len(dataBytes) == 0 || bytes.Equal(dataBytes, NotFoundPlaceholderBytes) {
return ErrPlaceholder
}
err = encoding.Unmarshal(m.encoding, dataBytes, val)
if err != nil {
return fmt.Errorf("encoding.Unmarshal error: %v, key=%s, cacheKey=%s, type=%T, data=%s ",
err, key, cacheKey, val, dataBytes)
}
return nil
}
// Del delete data
func (m *memoryCache) Del(_ context.Context, keys ...string) error {
if len(keys) == 0 {
return nil
}
key := keys[0]
cacheKey, err := BuildCacheKey(m.KeyPrefix, key)
if err != nil {
return fmt.Errorf("build cache key error, err=%v, key=%s", err, key)
}
m.client.Del(cacheKey)
return nil
}
// MultiSet multiple set data
func (m *memoryCache) MultiSet(ctx context.Context, valueMap map[string]interface{}, expiration time.Duration) error {
var err error
for key, value := range valueMap {
err = m.Set(ctx, key, value, expiration)
if err != nil {
return err
}
}
return nil
}
// MultiGet multiple get data
func (m *memoryCache) MultiGet(ctx context.Context, keys []string, value interface{}) error {
valueMap := reflect.ValueOf(value)
var err error
for _, key := range keys {
object := m.newObject()
err = m.Get(ctx, key, object)
if err != nil {
continue
}
valueMap.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(object))
}
return nil
}
// SetCacheWithNotFound set not found
func (m *memoryCache) SetCacheWithNotFound(_ context.Context, key string) error {
cacheKey, err := BuildCacheKey(m.KeyPrefix, key)
if err != nil {
return fmt.Errorf("BuildCacheKey error: %v, key=%s", err, key)
}
ok := m.client.SetWithTTL(cacheKey, []byte(NotFoundPlaceholder), 0, DefaultNotFoundExpireTime)
if !ok {
return errors.New("SetWithTTL failed")
}
return nil
}