-
Notifications
You must be signed in to change notification settings - Fork 51
/
option.go
177 lines (142 loc) · 4.3 KB
/
option.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cache
import (
"time"
"github.com/gin-gonic/gin"
)
// Config contains all options
type Config struct {
logger Logger
getCacheStrategyByRequest GetCacheStrategyByRequest
hitCacheCallback OnHitCacheCallback
missCacheCallback OnMissCacheCallback
beforeReplyWithCacheCallback BeforeReplyWithCacheCallback
singleFlightForgetTimeout time.Duration
shareSingleFlightCallback OnShareSingleFlightCallback
ignoreQueryOrder bool
prefixKey string
withoutHeader bool
discardHeaders []string
}
func newConfigByOpts(opts ...Option) *Config {
cfg := &Config{
logger: Discard{},
hitCacheCallback: defaultHitCacheCallback,
missCacheCallback: defaultMissCacheCallback,
beforeReplyWithCacheCallback: defaultBeforeReplyWithCacheCallback,
shareSingleFlightCallback: defaultShareSingleFlightCallback,
}
for _, opt := range opts {
opt(cfg)
}
return cfg
}
// Option represents the optional function.
type Option func(c *Config)
// WithLogger set the custom logger
func WithLogger(l Logger) Option {
return func(c *Config) {
if l != nil {
c.logger = l
}
}
}
// Logger define the logger interface
type Logger interface {
Errorf(string, ...interface{})
}
// Discard the default logger that will discard all logs of gin-cache
type Discard struct {
}
// Errorf will output the log at error level
func (l Discard) Errorf(string, ...interface{}) {
}
// WithCacheStrategyByRequest set up the custom strategy by per request
func WithCacheStrategyByRequest(getGetCacheStrategyByRequest GetCacheStrategyByRequest) Option {
return func(c *Config) {
if getGetCacheStrategyByRequest != nil {
c.getCacheStrategyByRequest = getGetCacheStrategyByRequest
}
}
}
// OnHitCacheCallback define the callback when use cache
type OnHitCacheCallback func(c *gin.Context)
var defaultHitCacheCallback = func(c *gin.Context) {}
// WithOnHitCache will be called when cache hit.
func WithOnHitCache(cb OnHitCacheCallback) Option {
return func(c *Config) {
if cb != nil {
c.hitCacheCallback = cb
}
}
}
// OnMissCacheCallback define the callback when use cache
type OnMissCacheCallback func(c *gin.Context)
var defaultMissCacheCallback = func(c *gin.Context) {}
// WithOnMissCache will be called when cache miss.
func WithOnMissCache(cb OnMissCacheCallback) Option {
return func(c *Config) {
if cb != nil {
c.missCacheCallback = cb
}
}
}
type BeforeReplyWithCacheCallback func(c *gin.Context, cache *ResponseCache)
var defaultBeforeReplyWithCacheCallback = func(c *gin.Context, cache *ResponseCache) {}
// WithBeforeReplyWithCache will be called before replying with cache.
func WithBeforeReplyWithCache(cb BeforeReplyWithCacheCallback) Option {
return func(c *Config) {
if cb != nil {
c.beforeReplyWithCacheCallback = cb
}
}
}
// OnShareSingleFlightCallback define the callback when share the singleflight result
type OnShareSingleFlightCallback func(c *gin.Context)
var defaultShareSingleFlightCallback = func(c *gin.Context) {}
// WithOnShareSingleFlight will be called when share the singleflight result
func WithOnShareSingleFlight(cb OnShareSingleFlightCallback) Option {
return func(c *Config) {
if cb != nil {
c.shareSingleFlightCallback = cb
}
}
}
// WithSingleFlightForgetTimeout to reduce the impact of long tail requests.
// singleflight.Forget will be called after the timeout has reached for each backend request when timeout is greater than zero.
func WithSingleFlightForgetTimeout(forgetTimeout time.Duration) Option {
return func(c *Config) {
if forgetTimeout > 0 {
c.singleFlightForgetTimeout = forgetTimeout
}
}
}
// IgnoreQueryOrder will ignore the queries order in url when generate cache key . This option only takes effect in CacheByRequestURI function
func IgnoreQueryOrder() Option {
return func(c *Config) {
c.ignoreQueryOrder = true
}
}
// WithPrefixKey will prefix the key
func WithPrefixKey(prefix string) Option {
return func(c *Config) {
c.prefixKey = prefix
}
}
func WithoutHeader() Option {
return func(c *Config) {
c.withoutHeader = true
}
}
func WithDiscardHeaders(headers []string) Option {
return func(c *Config) {
c.discardHeaders = headers
}
}
func CorsHeaders() []string {
return []string{
"Access-Control-Allow-Credentials",
"Access-Control-Expose-Headers",
"Access-Control-Allow-Origin",
"Vary",
}
}