-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskcache.go
381 lines (361 loc) · 9.53 KB
/
diskcache.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Package diskcache provides a http.RoundTripper implementation that can
// minify, compress, and cache HTTP responses retrieved using a standard
// http.Client on disk. Provides ability to define custom retention and storage
// policies depending on the host, path, or other URL components.
//
// Package diskcache does not aim to work as a on-disk HTTP proxy -- see
// github.com/gregjones/httpcache for a HTTP transport (http.RoundTripper)
// implementation that provides a RFC 7234 compliant cache.
//
// See _example/example.go for a more complete example.
package diskcache
import (
"bufio"
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"net/http/httputil"
"os"
"path"
"path/filepath"
"sort"
"time"
"github.com/spf13/afero"
"github.com/yookoala/realpath"
)
// Cache is a http.RoundTripper compatible disk cache.
type Cache struct {
transport http.RoundTripper
dirMode os.FileMode
fileMode os.FileMode
fs afero.Fs
noDefault bool
// matchers are the set of url matchers.
matchers []Matcher
// matcher is default matcher.
matcher *SimpleMatcher
}
// New creates a new disk cache.
//
// By default, the cache path will be <working directory>/cache. Change
// location using options.
func New(opts ...Option) (*Cache, error) {
m, err := NewSimpleMatcher(
`GET`,
`^(?P<proto>https?)://(?P<host>[^:]+)(?P<port>:[0-9]+)?$`,
`^/?(?P<path>.*)$`,
`{{proto}}/{{host}}{{port}}/{{path}}{{query}}`,
WithIndexPath("?index"),
WithQueryPrefix("_"),
WithLongPathHandler(func(key string) string {
if len(key) > 128 {
return fmt.Sprintf("?long/%x", sha256.Sum256([]byte(key)))
}
return key
}),
)
if err != nil {
return nil, err
}
c := &Cache{
dirMode: 0o755,
fileMode: 0o644,
matcher: m,
}
for _, o := range opts {
if err := o.apply(c); err != nil {
return nil, err
}
}
// set default fs as overlay at <working directory>/cache
if c.fs == nil {
dir, err := os.Getwd()
if err != nil {
return nil, err
}
if err := WithBasePathFs(filepath.Join(dir, "cache")).apply(c); err != nil {
return nil, err
}
}
// ensure body transformers are in order.
for _, v := range append(c.matchers, c.matcher) {
m, ok := v.(*SimpleMatcher)
if !ok {
continue
}
sort.Slice(m.policy.BodyTransformers, func(a, b int) bool {
return m.policy.BodyTransformers[a].TransformPriority() < m.policy.BodyTransformers[b].TransformPriority()
})
}
return c, nil
}
// RoundTrip satisfies the http.RoundTripper interface.
func (c *Cache) RoundTrip(req *http.Request) (*http.Response, error) {
// match policy for the request
key, p, err := c.Match(req)
if err != nil {
return nil, err
}
// no caching policy, pass to regular transport
if key == "" {
transport := c.transport
if transport == nil {
transport = http.DefaultTransport
}
return transport.RoundTrip(req)
}
force := false
for {
// fetch
stale, mod, res, err := c.Fetch(key, p, req, force)
switch {
case err != nil:
return nil, err
case p.Validator == nil:
return res, nil
}
// validate response
validity, err := p.Validator.Validate(req, res, mod, stale)
switch {
case err != nil:
return nil, err
case validity == Error:
return nil, fmt.Errorf("%T returned no error, but returned Error validity", p.Validator)
case validity == Retry:
force = true
case validity == Valid:
return res, nil
default:
return nil, fmt.Errorf("unable to handle %T validity %d", p.Validator, validity)
}
}
}
// Match finds the first matching cache policy for the request.
func (c *Cache) Match(req *http.Request) (string, Policy, error) {
matchers := c.matchers
if !c.noDefault {
matchers = append(matchers, c.matcher)
}
for _, m := range matchers {
key, p, err := m.Match(req)
if err != nil {
return "", Policy{}, err
}
if key != "" {
return key, p, nil
}
}
return "", Policy{}, nil
}
// Evict forces a cache eviction (deletion) for the key matching the request.
func (c *Cache) Evict(req *http.Request) error {
key, _, err := c.Match(req)
if err != nil {
return err
}
return c.EvictKey(key)
}
// EvictKey forces a cache eviction (deletion) of the specified key.
func (c *Cache) EvictKey(key string) error {
return c.fs.Remove(key)
}
// Fetch retrieves the key from the cache based on the policy TTL. When forced,
// or if the cached response is stale the request will be executed and the
// response cached.
func (c *Cache) Fetch(key string, p Policy, req *http.Request, force bool) (bool, time.Time, *http.Response, error) {
// check stale
stale, mod, err := c.Stale(req.Context(), key, p.TTL)
if err != nil {
return false, time.Time{}, nil, err
}
// exec when stale or forced
if stale || force {
res, err := c.Exec(key, p, req)
if err != nil {
return false, time.Time{}, nil, err
}
mod, err := c.Mod(key)
if err != nil {
return false, time.Time{}, nil, err
}
return false, mod, res, nil
}
// load
res, err := c.Load(key, p, req)
if err != nil {
return false, time.Time{}, nil, err
}
return true, mod, res, nil
}
// Mod returns last modified time of the key.
func (c *Cache) Mod(key string) (time.Time, error) {
fi, err := c.fs.Stat(key)
switch {
case err != nil:
return time.Time{}, err
case fi.IsDir():
return time.Time{}, fmt.Errorf("fs path %q is a directory", key)
}
return fi.ModTime(), nil
}
// Stale returns whether or not the key is stale, based on ttl.
func (c *Cache) Stale(ctx context.Context, key string, ttl time.Duration) (bool, time.Time, error) {
mod, err := c.Mod(key)
switch {
case err != nil && errors.Is(err, fs.ErrNotExist):
return true, mod, nil
case err != nil:
return false, time.Time{}, err
}
if d, ok := TTL(ctx); ok {
ttl = d
}
return ttl != 0 && time.Now().After(mod.Add(ttl)), mod, nil
}
// Cached returns whether or not the request is cached. Wraps Match, Stale.
func (c *Cache) Cached(req *http.Request) (bool, error) {
key, p, err := c.Match(req)
if err != nil {
return false, err
}
stale, _, err := c.Stale(req.Context(), key, p.TTL)
if err != nil {
return false, err
}
return !stale, nil
}
// Load unmarshals and loads the cached response for the key and cache policy.
func (c *Cache) Load(key string, p Policy, req *http.Request) (*http.Response, error) {
f, err := c.fs.OpenFile(key, os.O_RDONLY, 0)
if err != nil {
return nil, err
}
var r io.Reader = f
if p.MarshalUnmarshaler != nil {
buf := new(bytes.Buffer)
if err := p.MarshalUnmarshaler.Unmarshal(buf, f); err != nil {
return nil, err
}
r = buf
}
return http.ReadResponse(bufio.NewReader(r), req)
}
// Exec executes the request, storing the response using the key and cache
// policy. Applies header and body transformers, before marshaling and the
// response.
func (c *Cache) Exec(key string, p Policy, req *http.Request) (*http.Response, error) {
transport := c.transport
if transport == nil {
transport = http.DefaultTransport
}
// grab
res, err := transport.RoundTrip(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
// dump
buf, err := httputil.DumpResponse(res, false)
if err != nil {
return nil, err
}
// strip Transfer-Encoding and apply header transforms
buf = stripTransferEncodingHeader(buf)
for _, t := range p.HeaderTransformers {
buf = t.HeaderTransform(buf)
}
// apply body transforms
buf, err = transformAndAppend(
buf,
res.Body,
req.URL.String(),
res.StatusCode,
res.Header.Get("Content-Type"),
req.Method != "HEAD",
p.BodyTransformers...,
)
if err != nil {
return nil, err
}
body := buf
// marshal
if p.MarshalUnmarshaler != nil {
b := new(bytes.Buffer)
if err := p.MarshalUnmarshaler.Marshal(b, bytes.NewReader(buf)); err != nil {
return nil, err
}
buf = b.Bytes()
}
// store
if len(buf) != 0 {
// ensure path exists
if err := c.fs.MkdirAll(path.Dir(key), c.dirMode); err != nil {
return nil, err
}
// open cache file
f, err := c.fs.OpenFile(key, os.O_APPEND|os.O_CREATE|os.O_WRONLY|os.O_TRUNC, c.fileMode)
if err != nil {
return nil, err
}
if _, err := f.Write(buf); err != nil {
return nil, err
}
if err := f.Close(); err != nil {
return nil, err
}
}
// read response
return http.ReadResponse(bufio.NewReader(bytes.NewReader(body)), req)
}
// Policy is a disk cache policy.
type Policy struct {
// TTL is the time-to-live.
TTL time.Duration
// HeaderTransformers are the set of header transformers.
HeaderTransformers []HeaderTransformer
// BodyTransformers are the set of body tranformers.
BodyTransformers []BodyTransformer
// MarshalUnmarshaler is the marshal/unmarshaler responsible for storage on
// disk.
MarshalUnmarshaler MarshalUnmarshaler
// Validator validates responses.
Validator Validator
}
// UserCacheDir returns the user's system cache dir, adding paths to the end.
//
// Example usage:
//
// dir, err := diskcache.UserCacheDir("my-app-name")
// cache, err := diskcache.New(diskcache.WithBasePathFs(dir))
//
// Note: WithAppCacheDir is easier.
func UserCacheDir(paths ...string) (string, error) {
dir, err := os.UserCacheDir()
if err != nil {
return "", err
}
if dir, err = realpath.Realpath(dir); err != nil {
return "", err
}
return filepath.Join(append([]string{dir}, paths...)...), nil
}
// contextKey is a context key.
type contextKey string
// context keys.
const (
ttlKey contextKey = "ttl"
)
// WithContextTTL adds the ttl to the context.
func WithContextTTL(parent context.Context, ttl time.Duration) context.Context {
return context.WithValue(parent, ttlKey, ttl)
}
// TTL returns the ttl from the context.
func TTL(ctx context.Context) (time.Duration, bool) {
ttl, ok := ctx.Value(ttlKey).(time.Duration)
return ttl, ok
}