/
httpcache.go
112 lines (100 loc) · 2.65 KB
/
httpcache.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
// The autocamper package contains all of the brains for querying campsites.
package autocamper
import (
"bytes"
"encoding/gob"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/steveyen/gkvlite"
)
var (
// cachePath is the location of the cache
cachePath = os.ExpandEnv("${HOME}/.autocamper.cache")
// store is a gkvlite Store
store = getCacheStore()
// collection is a gkvlite collection that documents can be fetched from.
collection = store.SetCollection("cache", nil)
)
// CachedHttpResponse defines which data may be cached for an HTTP response.
type CachedHttpResponse struct {
// StatusCode is the HTTP status code value (ex: 400)
StatusCode int
// Body is the entire HTTP message body.
Body []byte
// MTime is when this value was last updated in the cache.
MTime time.Time
}
// cachedFetch wraps http.Get/http.Post behind a persistent cache.
func cachedFetch(url string, v url.Values, maxAge time.Duration) (CachedHttpResponse, error) {
key := []byte(url + "?" + v.Encode())
cachedBytes, err := collection.Get(key)
// Item is in cache, but we do not yet know if it is too old.
if cachedBytes != nil {
log.Printf("FOUND: %s", key)
var cr CachedHttpResponse
buf := bytes.NewBuffer(cachedBytes)
dec := gob.NewDecoder(buf)
err = dec.Decode(&cr)
// Invalid item in cache?
if err != nil {
log.Printf("Failed to decode: %s", err)
} else {
age := time.Since(cr.MTime)
log.Printf("Item age is %s", age)
if age < maxAge {
log.Printf("HIT: %s")
return cr, nil
} else {
log.Printf("MISS (TOO OLD): %s", key)
}
}
}
// It's a miss.
log.Printf("MISS: %s", key)
// GET vs POST
var r *http.Response
if v != nil {
r, err = http.PostForm(url, v)
} else {
r, err = http.Get(url)
}
// Write the response into the cache. Mask over any failures.
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return CachedHttpResponse{}, err
}
cr := CachedHttpResponse{StatusCode: r.StatusCode, Body: body, MTime: time.Now()}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err = enc.Encode(&cr)
if err != nil {
log.Printf("Failed to encode response: %s", err)
} else {
bufBytes, err := ioutil.ReadAll(&buf)
log.Printf("Buf bytes: %s", bufBytes)
if err != nil {
log.Printf("Failed to read back encoded response: %s", err)
} else {
log.Printf("Storing %s", key)
collection.Set(key, bufBytes)
store.Flush()
}
}
return cr, nil
}
// Returns a gkvlite collection
func getCacheStore() *gkvlite.Store {
f, err := os.OpenFile(cachePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
log.Fatal(err)
}
s, err := gkvlite.NewStore(f)
if err != nil {
log.Fatal(err)
}
return s
}