-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
186 lines (162 loc) · 4.42 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
package fscache
import (
"container/heap"
"errors"
"io/ioutil"
"log"
"math"
"os"
"path/filepath"
"time"
)
// Interface provides a set of general cache functions.
type Interface interface {
// Set sets the value of key as src.
// Setting the same key multiple times, the last set call takes effect.
Set(key string, src []byte) error
// Get gets the value of key to dst, and returns dst no matter whether or not there is an error.
Get(key string, dst []byte) ([]byte, error)
// Has tells you if a key has been set or not.
Has(key string) bool
}
var (
// ErrNotFound will be returned when getting a key that not setting before.
ErrNotFound = errors.New("not found")
)
// Cache is a LRU filesystem cache based on atime.
type Cache struct {
cacheDir string
maxBytes int64
gcInterval time.Duration
logger Logger
fih fileInfoHeap
gcStopCh <-chan struct{}
}
func (f *Cache) filedir() string { return filepath.Join(f.cacheDir, "cache") }
func (f *Cache) tmpdir() string { return filepath.Join(f.cacheDir, "tmp") }
func (f *Cache) filepath(key string) string { return filepath.Join(f.filedir(), key) }
func (f *Cache) tmppath(key string) string { return filepath.Join(f.tmpdir(), key) }
// Option can be passed to New() to tailor your needs.
type Option func(fc *Cache)
// WithCacheDir specifies where the cache holds.
func WithCacheDir(cacheDir string) Option { return func(fc *Cache) { fc.cacheDir = cacheDir } }
// WithMaxBytes specifies how many space the cache could take up.
func WithMaxBytes(bytes int64) Option { return func(fc *Cache) { fc.maxBytes = bytes } }
// WithGcStopCh receives a channel, when the channel close, gc will stop.
// By default, gc will not stop until the process exits.
func WithGcStopCh(stopCh <-chan struct{}) Option { return func(fc *Cache) { fc.gcStopCh = stopCh } }
// WithGcInterval specifies how often the GC performs.
func WithGcInterval(interval time.Duration) Option {
return func(fc *Cache) { fc.gcInterval = interval }
}
// Logger used by this package.
type Logger interface {
Errorf(fmt string, args ...interface{})
}
type logger struct {
log.Logger
}
func (l *logger) Errorf(fmt string, args ...interface{}) { log.Printf(fmt, args...) }
// New creates a LRU filesystem cache based on atime, and starts the GC goroutine.
func New(opts ...Option) (Interface, error) {
fc := &Cache{
cacheDir: os.TempDir(),
maxBytes: math.MaxInt64,
gcInterval: 5 * time.Minute,
logger: &logger{},
gcStopCh: make(chan struct{}),
}
for _, opt := range opts {
opt(fc)
}
if err := os.MkdirAll(fc.filedir(), 0775); err != nil {
return nil, err
}
if err := os.MkdirAll(fc.tmpdir(), 0775); err != nil {
return nil, err
}
if fc.maxBytes > 0 {
go fc.gcRunner()
}
return fc, nil
}
func (f *Cache) gcRunner() {
ticker := time.NewTicker(f.gcInterval)
defer ticker.Stop()
for {
select {
case <-f.gcStopCh:
return
case <-ticker.C:
f.gc()
}
}
}
func (f *Cache) gc() {
curBytes := int64(0)
f.fih = nil
err := filepath.Walk(f.filedir(), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
curBytes += info.Size()
heap.Push(&f.fih, info)
return nil
})
if err != nil {
f.logger.Errorf("gc walk dir %s : %s", f.filedir(), err)
return
}
if curBytes <= f.maxBytes {
return
}
var (
needGcBytes = curBytes - f.maxBytes
bytesSoFar int64
keysToGc []string
)
for bytesSoFar < needGcBytes {
fi := heap.Pop(&f.fih).(os.FileInfo)
bytesSoFar += fi.Size()
keysToGc = append(keysToGc, fi.Name())
}
for _, k := range keysToGc {
fp := f.filepath(k)
if err := os.Remove(fp); err != nil {
f.logger.Errorf("gc %s : %s", fp, err)
return
}
}
}
// Set implements Interface.Set().
func (f *Cache) Set(key string, src []byte) error {
return atomicWriteFile(f.filepath(key), f.tmppath(key), src, 0644)
}
// Get implements Interface.Get().
func (f *Cache) Get(key string, dst []byte) ([]byte, error) {
fp := f.filepath(key)
src, err := ioutil.ReadFile(fp)
if err != nil {
if os.IsNotExist(err) {
return dst, ErrNotFound
}
return dst, err
}
fi, err := os.Stat(fp)
if err != nil {
return dst, err
}
if err := os.Chtimes(fp, time.Now(), fi.ModTime()); err != nil {
return dst, err
}
dst = append(dst, src...)
return dst, nil
}
// Has implements Interface.Has().
func (f *Cache) Has(key string) bool {
_, err := os.Stat(f.filepath(key))
return err == nil
}