-
Notifications
You must be signed in to change notification settings - Fork 79
/
cache.go
89 lines (73 loc) · 1.93 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
package tigertonic
import (
"fmt"
"net/http"
"strings"
"time"
)
const ONE_YEAR_IN_HOURS = time.Hour * 24 * 365
// CacheControl is an http.Handler that sets cache headers.
type CacheControl struct {
options CacheOptions
handler http.Handler
}
// Cached returns an http.Handler that sets appropriate Cache headers on
// the outgoing response and passes requests to a wrapped http.Handler.
func Cached(handler http.Handler, o CacheOptions) *CacheControl {
return &CacheControl{
handler: handler,
options: o,
}
}
// ServeHTTP sets the header and passes the request and response to the
// wrapped http.Handler
func (c *CacheControl) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.handler.ServeHTTP(w, r)
if w.Header().Get("Cache-Control") == "" {
w.Header().Set("Cache-Control", c.options.String())
}
}
// These set the relevant headers in the response per
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
type CacheOptions struct {
Immutable bool
IsPrivate bool
NoCache bool
NoStore bool
NoTransform bool
MustRevalidate bool
ProxyRevalidate bool
MaxAge time.Duration
SharedMaxAge time.Duration
}
func (o CacheOptions) String() string {
elements := make([]string, 0)
if o.Immutable {
o.MaxAge = ONE_YEAR_IN_HOURS
}
if o.IsPrivate {
elements = append(elements, "private")
}
if o.NoCache {
elements = append(elements, "no-cache")
}
if o.NoStore {
elements = append(elements, "no-store")
}
if o.NoTransform {
elements = append(elements, "no-transform")
}
if o.MustRevalidate {
elements = append(elements, "must-revalidate")
}
if o.ProxyRevalidate {
elements = append(elements, "proxy-revalidate")
}
if o.MaxAge != 0 {
elements = append(elements, fmt.Sprintf("max-age=%.0f", o.MaxAge.Seconds()))
}
if o.SharedMaxAge != 0 {
elements = append(elements, fmt.Sprintf("s-maxage=%.0f", o.SharedMaxAge.Seconds()))
}
return strings.Join(elements, ", ")
}