-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathoptions.go
More file actions
114 lines (101 loc) · 3.21 KB
/
options.go
File metadata and controls
114 lines (101 loc) · 3.21 KB
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
package stampede
import (
"time"
)
type Options struct {
// TTL is the time-to-live for the cache. NOTE: if this is not set,
// then we use the package-level default of 1 minute. You can override
// by passing `WithTTL(time.Second * 10)` to stampede.Do(), or passing
// `WithSkipCache(true)` to skip the cache entirely.
//
// Default: 1 minute
TTL time.Duration
// SkipCache is a flag that determines whether the cache should be skipped.
// If true, the cache will not be used, but the request will still use
// singleflight request coalescing.
//
// Default: false
SkipCache bool
// HTTPCacheKeyRequestBody is a flag that determines whether the request body
// should be used to generate the cache key. This is useful for varying the cache
// key based on request headers.
//
// Default: true
HTTPCacheKeyRequestBody bool
// HTTPCacheKeyRequestHeaders is a list of headers that will be used to generate
// the cache key. This ensures we use the request body contents so we can properly
// cache different requests that have the same URL but different query params or
// body content.
//
// Default: []
HTTPCacheKeyRequestHeaders []string
// HTTPStatusTTL is a function that returns the time-to-live for a given HTTP
// status code. This allows you to customize the TTL for different HTTP status codes.
//
// Default: nil
HTTPStatusTTL func(status int) time.Duration
}
// WithTTL sets the TTL for the cache.
//
// Default: 1 minute
func WithTTL(ttl time.Duration) Option {
return func(o *Options) {
o.TTL = ttl
}
}
// WithSkipCache sets the SkipCache flag. If true, the cache will not be used,
// but the request will still use singleflight request coalescing.
//
// Default: false
func WithSkipCache(skip bool) Option {
return func(o *Options) {
o.SkipCache = skip
}
}
// WithHTTPStatusTTL sets the HTTPStatusTTL function. This allows you to
// customize the TTL for different HTTP status codes.
//
// Default: nil
func WithHTTPStatusTTL(fn func(status int) time.Duration) Option {
return func(o *Options) {
o.HTTPStatusTTL = fn
}
}
// WithHTTPCacheKeyRequestBody sets the HTTPCacheKeyRequestBody flag. This
// ensures we use the request body contents so we can properly cache different
// requests that have the same URL but different query params or body content.
//
// Default: true
func WithHTTPCacheKeyRequestBody(b bool) Option {
return func(o *Options) {
o.HTTPCacheKeyRequestBody = b
}
}
// WithHTTPCacheKeyRequestHeaders sets the HTTPCacheKeyRequestHeaders list.
// This is useful for varying the cachekey based on request headers.
//
// Default: []
func WithHTTPCacheKeyRequestHeaders(headers []string) Option {
return func(o *Options) {
o.HTTPCacheKeyRequestHeaders = headers
}
}
type Option func(*Options)
// getOptions returns a new Options with the given ttl and options,
// and also applies default values for any options that are not set.
func getOptions(ttl time.Duration, options ...Option) *Options {
if ttl == 0 {
ttl = DefaultCacheTTL
}
opts := &Options{
TTL: ttl,
SkipCache: false,
HTTPStatusTTL: nil,
HTTPCacheKeyRequestHeaders: nil,
HTTPCacheKeyRequestBody: true,
}
for _, o := range options {
o(opts)
}
return opts
}