-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
116 lines (89 loc) · 2.45 KB
/
cache.go
File metadata and controls
116 lines (89 loc) · 2.45 KB
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
package irdata
import (
"crypto/md5"
"errors"
"time"
"git.mills.io/prologic/bitcask"
log "github.com/sirupsen/logrus"
)
const _maxValueSize = 1024 * 1024 * 256 // 256MB
const _maxKeySize = 1024 * 4 // 4K
type hashedKey []byte
func (i *Irdata) cacheOpen(cacheDir string) error {
var err error
options := []bitcask.Option{
bitcask.WithMaxValueSize(_maxValueSize),
bitcask.WithMaxKeySize(_maxKeySize),
bitcask.WithSync(true),
}
if i.cacheMaxDatafileSize > 0 {
options = append(options, bitcask.WithMaxDatafileSize(i.cacheMaxDatafileSize))
}
i.cask, err = bitcask.Open(
cacheDir,
options...,
)
return err
}
func (i *Irdata) cacheClose() {
// call close no matter what
defer i.cask.Close()
log.Info("Running cache cleanup")
err := i.cask.RunGC()
if err != nil {
log.WithField("err", err).Info("cask.RunGC failed")
}
log.Debug("Merging cache")
// Merge is compaction. If it hits an expired key, it may return ErrKeyExpired.
// Since we run GC right before this, it's usually transient or due to keys
// expiring during the merge itself. We retry up to 3 times to ensure compaction
// completes as it is a critical maintenance task.
for attempts := 1; attempts <= 3; attempts++ {
err = i.cask.Merge()
if err == nil {
break
}
if errors.Is(err, bitcask.ErrKeyExpired) {
log.WithField("attempt", attempts).Debug("cask.Merge hit expired keys, retrying...")
_ = i.cask.RunGC()
continue
}
log.WithField("err", err).Warn("cask.Merge failed")
break
}
log.Info("Done")
}
func (i *Irdata) cacheCloseFast() {
log.Info("Closing cache (fast)")
i.cask.Close()
}
func hashKey(key string) hashedKey {
hash := md5.Sum([]byte(key))
return hash[:]
}
func (i *Irdata) getCachedData(key string) ([]byte, error) {
data, err := i.cask.Get(hashKey(key))
if errors.Is(err, bitcask.ErrKeyExpired) || errors.Is(err, bitcask.ErrKeyNotFound) {
return nil, nil
} else if err != nil {
return nil, makeErrorf("cache get error for %s [%v]", key, err)
}
return data, nil
}
func (i *Irdata) setCachedData(key string, data []byte, ttl time.Duration) error {
err := i.cask.PutWithTTL(hashKey(key), data, ttl)
if err != nil {
return makeErrorf("cache put error for %s [%v]", key, err)
}
return nil
}
func (i *Irdata) deleteCachedData(key string) error {
k := hashKey(key)
if i.cask.Has(k) {
err := i.cask.Delete(k)
if err != nil {
return makeErrorf("cache delete error for %s [%v]", key, err)
}
}
return nil
}