/
cache.go
118 lines (95 loc) · 2.03 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
package aliyundrive
import (
"bytes"
"encoding/gob"
"github.com/allegro/bigcache/v3"
"github.com/sirupsen/logrus"
"strings"
"time"
)
type bigCache struct {
cache *bigcache.BigCache
}
type bigCacheOptions struct {
size int
ttl time.Duration
cleanFreq time.Duration
}
func newBigCache(options *bigCacheOptions) (*bigCache, error) {
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
cache, err := bigcache.NewBigCache(bigcache.Config{
Shards: 1024,
LifeWindow: options.ttl,
CleanWindow: options.cleanFreq,
MaxEntriesInWindow: 100,
MaxEntrySize: 500,
Verbose: false,
HardMaxCacheSize: options.size,
StatsEnabled: false,
})
if err != nil {
return nil, err
}
return &bigCache{
cache: cache,
}, nil
}
func (b *bigCache) Get(key string) (interface{}, error) {
value, err := b.cache.Get(key)
if err != nil {
return nil, err
}
v, err := deserialize(value)
if err != nil {
return nil, err
}
return v, nil
}
func (b *bigCache) Set(key string, value interface{}) error {
valueBytes, err := serialize(value)
if err != nil {
logrus.Errorf("serialize error %s", err)
return err
}
err = b.cache.Set(key, valueBytes)
if err != nil {
return err
}
return nil
}
func (b *bigCache) RemoveWithPrefix(prefix string) int {
iterator := b.cache.Iterator()
count := 0
for iterator.SetNext() {
value, err := iterator.Value()
if err != nil {
return count
}
if strings.HasPrefix(value.Key(), prefix) {
_ = b.cache.Delete(value.Key())
count++
}
}
return count
}
func serialize(value interface{}) ([]byte, error) {
buffer := bytes.Buffer{}
enc := gob.NewEncoder(&buffer)
gob.Register(value)
err := enc.Encode(&value)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
func deserialize(valueBytes []byte) (interface{}, error) {
var value interface{}
buf := bytes.NewBuffer(valueBytes)
dec := gob.NewDecoder(buf)
err := dec.Decode(&value)
if err != nil {
return nil, err
}
return value, nil
}