-
Notifications
You must be signed in to change notification settings - Fork 0
/
artcache.go
185 lines (156 loc) · 3.63 KB
/
artcache.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
package main
import (
"bytes"
"container/list"
"fmt"
"image"
"image/jpeg"
"io/ioutil"
"os"
"path"
"sync"
"time"
"github.com/peterbourgon/diskv" // roll our own disk based LRU
)
type entry struct {
key string
expires int64
}
// CACache wraps the k/v store cache insclusive LRU mechanism
type CACache struct {
conn *diskv.Diskv
MaxAge int64
mu sync.Mutex
lru *list.List // Front is least-recent
cache map[string]*list.Element
size int64
}
// InitImageCache initiates the diskv client
func InitImageCache(base string, cleanup bool) *CACache {
cac := &CACache{
conn: diskv.New(diskv.Options{
BasePath: base,
CacheSizeMax: 10 * 1024 * 1024,
}),
MaxAge: 2 * 60 * 60,
lru: list.New(),
cache: make(map[string]*list.Element),
}
if cleanup {
_ = cac.cleanup()
}
return cac
}
func (car *CACache) isOlder(t time.Time, d time.Duration) bool {
return time.Now().Sub(t) > d
}
func (car *CACache) cleanup() (err error) {
tmpfiles, err := ioutil.ReadDir(car.conn.BasePath)
if err != nil {
return
}
for _, file := range tmpfiles {
if file.Mode().IsRegular() {
if car.isOlder(file.ModTime(), 24*time.Hour) {
err = os.Remove(path.Join(car.conn.BasePath, file.Name()))
if err != nil {
return
}
}
}
}
return
}
// Close the diskv client connection
func (car *CACache) Close() {
fmt.Println(`not implemented`)
}
// Get returns the response corresponding to key if present.
func (car *CACache) Get(key string) (resp []byte, ok bool) {
ok = car.conn.Has(key)
if ok {
resp, _ = car.conn.Read(key)
le, okc := car.cache[key]
if !okc {
car.setLRU(key) // back-fill orphans
return resp, ok
}
car.mu.Lock()
if car.MaxAge > 0 && le.Value.(*entry).expires <= time.Now().Unix() {
car.deleteElement(le)
car.maybeDeleteOldest()
car.mu.Unlock() // Avoiding defer overhead
return nil, false
}
car.lru.MoveToBack(le)
car.mu.Unlock() // Avoiding defer overhead
}
return resp, ok
}
// GetImage returns the image response corresponding to key if present.
func (car *CACache) GetImage(key string) (im image.Image, ok bool) {
resp, ok := car.Get(key)
if !ok {
return nil, ok
}
im, _, err := image.Decode(bytes.NewReader(resp))
if err != nil {
fmt.Println(`caught`, err)
return nil, false
}
return im, true
}
// Set saves a response to the cache as key.
func (car *CACache) Set(key string, resp []byte) {
err := car.conn.Write(key, resp)
if nil != err {
fmt.Println(`set caught`, err)
}
car.setLRU(key)
}
func (car *CACache) setLRU(key string) {
// LRU mech.
expires := int64(0)
if car.MaxAge > 0 {
expires = time.Now().Unix() + car.MaxAge
}
car.mu.Lock()
if le, ok := car.cache[key]; ok {
car.lru.MoveToBack(le)
e := le.Value.(*entry)
e.expires = expires
} else {
e := &entry{key: key, expires: expires}
car.cache[key] = car.lru.PushBack(e)
}
car.maybeDeleteOldest()
car.mu.Unlock()
}
// SetImage saves an image to the keyyed cache.
func (car *CACache) SetImage(key string, im image.Image) {
buff := new(bytes.Buffer)
// note low quality - its an rgb panel so save the bytes
err := jpeg.Encode(buff, im, &jpeg.Options{Quality: 70})
if nil == err {
car.Set(key, buff.Bytes())
} else {
fmt.Println(`caught`, err)
}
}
func (car *CACache) maybeDeleteOldest() {
if car.MaxAge > 0 {
now := time.Now().Unix()
for le := car.lru.Front(); le != nil && le.Value.(*entry).expires <= now; le = car.lru.Front() {
car.deleteElement(le)
}
}
}
func (car *CACache) deleteElement(le *list.Element) {
car.lru.Remove(le)
e := le.Value.(*entry)
delete(car.cache, e.key)
err := car.conn.Erase(e.key)
if nil != err {
fmt.Println(`caught`, err)
}
}