/
disks3cache.go
59 lines (51 loc) · 1.41 KB
/
disks3cache.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
package disks3cache
import (
"io/ioutil"
"github.com/going/toolkit/log"
"github.com/gregjones/httpcache/diskcache"
"github.com/peterbourgon/diskv"
"github.com/victortrac/disks3cache/s3cache"
)
type Cache struct {
disk *diskcache.Cache
s3 *s3cache.Cache
}
func (c *Cache) Get(key string) (resp []byte, ok bool) {
// Check disk first
resp, ok = c.disk.Get(key)
if ok == true {
log.Debugf("Found %v in disk cache", key)
return resp, ok
}
resp, ok = c.s3.Get(key)
if ok == true {
log.Debugf("Found %v in s3 cache: %v", key, s3cache.CacheKeyToObjectKey(key))
go c.disk.Set(key, resp)
return resp, ok
}
log.Debugf("%v not found in cache: %v", key, s3cache.CacheKeyToObjectKey(key))
return []byte{}, ok
}
func (c *Cache) Set(key string, resp []byte) {
log.Debugf("Setting key %v on disk and s3: %v", key, s3cache.CacheKeyToObjectKey(key))
go c.disk.Set(key, resp)
go c.s3.Set(key, resp)
}
func (c *Cache) Delete(key string) {
log.Debugf("Deleting key %v on disk and s3: %v", key, s3cache.CacheKeyToObjectKey(key))
go c.disk.Delete(key)
go c.s3.Delete(key)
}
func New(cacheDir string, cacheSize uint64, bucketURL string) *Cache {
if cacheDir == "" {
cacheDir, _ = ioutil.TempDir("", "disks3cache")
}
dv := diskv.New(diskv.Options{
BasePath: cacheDir,
CacheSizeMax: cacheSize * 1024 * 1024,
})
return &Cache{
disk: diskcache.NewWithDiskv(dv),
s3: s3cache.New(bucketURL),
}
}