-
Notifications
You must be signed in to change notification settings - Fork 8
/
cache.go
63 lines (57 loc) · 1.33 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
package main
import (
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"time"
)
func NewCache(duration time.Duration, next http.Handler) *Cache {
return &Cache{
urlToResponse: make(map[string]CacheResponse),
Next: next,
Duration: duration,
}
}
type Cache struct {
urlToResponse map[string]CacheResponse
Next http.Handler
Duration time.Duration
}
type CacheResponse struct {
Header http.Header
Body []byte
Expiry time.Time
}
func (c *Cache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet || r.Method == http.MethodOptions || r.Method == http.MethodHead {
cr, inCache := c.urlToResponse[r.URL.String()]
if inCache && time.Now().Before(cr.Expiry) {
log.Printf("%s: cache hit\n", r.URL.String())
} else {
log.Printf("%s: setting up cache\n", r.URL.String())
// Make the response and cache it.
ww := httptest.NewRecorder()
c.Next.ServeHTTP(ww, r)
body, _ := ioutil.ReadAll(ww.Body)
cr = CacheResponse{
Body: body,
Header: ww.Header(),
Expiry: time.Now().Add(c.Duration),
}
c.urlToResponse[r.URL.String()] = cr
}
// Copy headers.
for k, v := range cr.Header {
for _, vv := range v {
w.Header().Set(k, vv)
}
}
// Write body.
if cr.Body != nil {
w.Write(cr.Body)
}
return
}
c.Next.ServeHTTP(w, r)
}