-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachecontrol.go
122 lines (98 loc) · 2.96 KB
/
cachecontrol.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
package cachecontrol
import (
"fmt"
"strings"
"time"
"github.com/gin-gonic/gin"
)
const CacheControlHeader = "Cache-Control"
// Config defines a cache-control configuration.
//
// References:
// https://datatracker.ietf.org/doc/html/rfc7234#section-5.2.2
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
type Config struct {
MustRevalidate bool
NoCache bool
NoStore bool
NoTransform bool
Public bool
Private bool
ProxyRevalidate bool
MaxAge *time.Duration
SMaxAge *time.Duration
Immutable bool
StaleWhileRevalidate *time.Duration
StaleIfError *time.Duration
}
func (c *Config) buildCacheControl() string {
var values []string
if c.MustRevalidate {
values = append(values, "must-revalidate")
}
if c.NoCache {
values = append(values, "no-cache")
}
if c.NoStore {
values = append(values, "no-store")
}
if c.NoTransform {
values = append(values, "no-transform")
}
if c.Public {
values = append(values, "public")
}
if c.Private {
values = append(values, "private")
}
if c.ProxyRevalidate {
values = append(values, "proxy-revalidate")
}
if c.MaxAge != nil {
values = append(values, fmt.Sprintf("max-age=%.f", c.MaxAge.Seconds()))
}
if c.SMaxAge != nil {
values = append(values, fmt.Sprintf("s-maxage=%.f", c.SMaxAge.Seconds()))
}
if c.Immutable {
values = append(values, "immutable")
}
if c.StaleWhileRevalidate != nil {
values = append(values, fmt.Sprintf("stale-while-revalidate=%.f", c.StaleWhileRevalidate.Seconds()))
}
if c.StaleIfError != nil {
values = append(values, fmt.Sprintf("stale-if-error=%.f", c.StaleIfError.Seconds()))
}
return strings.Join(values, ", ")
}
func (c *Config) apply(ginCtx *gin.Context, value string) {
header := ginCtx.Writer.Header()
header.Set(CacheControlHeader, value)
}
// New creates a new Gin middleware which generates a cache-control header.
// Existing cache-control headers are removed.
// Other caching-related headers, such as `Expires` and `Pragma`, remain unchanged.
func New(config Config) gin.HandlerFunc {
value := config.buildCacheControl()
return func(ginCtx *gin.Context) {
config.apply(ginCtx, value)
}
}
// Duration is a helper function which returns a time.Duration pointer.
func Duration(duration time.Duration) *time.Duration {
return &duration
}
// NoCachePreset is a cache-control configuration preset which advices the HTTP client not to cache at all.
var NoCachePreset = Config{
MustRevalidate: true,
NoCache: true,
NoStore: true,
}
// CacheAssetsForeverPreset is a cache-control configuration preset which advices the HTTP client
// and all caches in between to cache the object forever without revalidation.
// Technically, "forever" means 1 year, in order to comply with common CDN limits.
var CacheAssetsForeverPreset = Config{
Public: true,
MaxAge: Duration(8760 * time.Hour),
Immutable: true,
}