-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
45 lines (40 loc) · 1.23 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
package middleware
import (
"strconv"
"github.com/Nigel2392/router/v3"
"github.com/Nigel2392/router/v3/request"
)
// Set the cache headers for the response.
// This will enable caching for the specified amount of seconds.
func Cache(maxAge int) func(next router.Handler) router.Handler {
return func(next router.Handler) router.Handler {
return router.HandleFunc(func(r *request.Request) {
for _, header := range etagHeaders {
r.Response.Header().Del(header)
}
r.Response.Header().Set("Cache-Control", "public, max-age="+strconv.Itoa(maxAge))
next.ServeHTTP(r)
})
}
}
var etagHeaders = []string{
"ETag",
"If-Modified-Since",
"If-Match",
"If-None-Match",
"If-Range",
"If-Unmodified-Since",
}
// Set the cache headers for the response.
// This will disable caching.
func NoCache(next router.Handler) router.Handler {
return router.HandleFunc(func(r *request.Request) {
for _, header := range etagHeaders {
r.Response.Header().Del(header)
}
r.Response.Header().Set("Cache-Control", "no-cache, no-store, no-transform, must-revalidate, private, max-age=0")
r.Response.Header().Set("Pragma", "no-cache")
r.Response.Header().Set("Expires", "0")
next.ServeHTTP(r)
})
}