-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
163 lines (136 loc) · 3.42 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
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
package cachewrapper
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, opts ...optionFunc) *CacheControl {
co := CacheOptions{}
for _, o := range opts {
o(&co)
}
return &CacheControl{
handler: handler,
options: co,
}
}
// 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) {
w.Header().Set("Cache-Control", c.options.String())
c.handler.ServeHTTP(w, r)
}
// MaxAge configures the maximum-age cache option.
func MaxAge(d time.Duration) optionFunc {
return func(o *CacheOptions) {
o.MaxAge = d
}
}
// NoTransform configures the the no-transform pragma.
func NoTransform() optionFunc {
return func(o *CacheOptions) {
o.NoTransform = true
}
}
// Immutable configures the max-age pragma to be one year in the future.
func Immutable() optionFunc {
return func(o *CacheOptions) {
o.Immutable = true
}
}
// Private configures the private cache option.
func Private() optionFunc {
return func(o *CacheOptions) {
o.Private = true
}
}
// NoCache configures the no-cache pragma.
func NoCache() optionFunc {
return func(o *CacheOptions) {
o.NoCache = true
}
}
// NoStore configures the no-store pragma.
func NoStore() optionFunc {
return func(o *CacheOptions) {
o.NoStore = true
}
}
// MustRevalidate configures the must-revalidate pragma.
func MustRevalidate() optionFunc {
return func(o *CacheOptions) {
o.MustRevalidate = true
}
}
// ProxyRevalidate configures the proxy-revalidate pragma.
func ProxyRevalidate() optionFunc {
return func(o *CacheOptions) {
o.ProxyRevalidate = true
}
}
// SharedMaxAge configures the s-maxage pragma.
func SharedMaxAge(d time.Duration) optionFunc {
return func(o *CacheOptions) {
o.SharedMaxAge = d
}
}
// Config takes a CacheOptions value and replaces options.
func Config(co CacheOptions) optionFunc {
return func(o *CacheOptions) {
*o = co
}
}
// These set the relevant pragmas in the response per
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
type CacheOptions struct {
Immutable bool
Private 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.Private {
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, ", ")
}
type optionFunc func(o *CacheOptions)