-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
108 lines (95 loc) · 2.11 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
package x
import (
"io"
"os"
"path/filepath"
"time"
"github.com/jpillora/maplock"
)
type Cache struct {
locks maplock.Maplock
Base string
}
var defaultCache = Cache{
Base: "cache-pzlr",
}
func NewCache(name string) *Cache {
return defaultCache.Fork(name)
}
func (c *Cache) Fork(name string) *Cache {
return &Cache{
Base: c.Base + "-" + name,
}
}
func (c *Cache) NetCached(domain, id string, fn func() (io.ReadCloser, error)) (io.ReadCloser, error) {
var expires time.Duration
if Online(domain) {
expires = 2 * time.Minute
}
return c.Cached(id, expires, fn)
}
func (c *Cache) Cached(id string, expires time.Duration, fn func() (io.ReadCloser, error)) (io.ReadCloser, error) {
c.locks.Lock(id)
defer c.locks.Unlock(id)
file := c.Base + "-" + id + ".bin"
path := filepath.Join(os.TempDir(), file)
// already cached to disk within the last day, return file as the read-closer
s, err := os.Stat(path)
if err == nil && !s.IsDir() {
ready := expires == 0 || time.Since(s.ModTime()) < expires
if ready {
f, err := os.Open(path)
if err == nil {
return f, nil
}
}
}
// not cached, return the function (likely a network request)
rc, err := fn()
if err != nil {
return rc, err
}
if rc == nil {
return nil, nil
}
// prepare tee file
tf, err := os.Create(path)
if err != nil {
return rc, nil // ignore cache file errors
}
// return read closer which also tees of to a tmp file
return &teeReadCloser{
teeFile: tf,
rc: rc,
}, nil
}
func NetCached(domain, id string, fn func() (io.ReadCloser, error)) (io.ReadCloser, error) {
return defaultCache.NetCached(domain, id, fn)
}
type teeReadCloser struct {
teeFile *os.File
rc io.ReadCloser
}
func (r *teeReadCloser) Read(p []byte) (int, error) {
if r.rc == nil {
return 0, io.EOF
}
n, err := r.rc.Read(p)
if n > 0 && r.teeFile != nil {
r.teeFile.Write(p[:n]) // ignore cache file write errors
}
if err == io.EOF {
r.closeTee()
}
return n, err
}
func (r *teeReadCloser) Close() error {
r.closeTee()
return r.rc.Close()
}
func (r *teeReadCloser) closeTee() {
if t := r.teeFile; t != nil {
r.teeFile = nil
t.Close()
}
}