You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/stores/cache/cacheopt.go

50 lines
959 B
Go

4 years ago
package cache
import "time"
4 years ago
const (
defaultExpiry = time.Hour * 24 * 7
defaultNotFoundExpiry = time.Minute
4 years ago
)
type (
// An Options is used to store the cache options.
Options struct {
Expiry time.Duration
NotFoundExpiry time.Duration
}
// Option defines the method to customize an Options.
Option func(o *Options)
)
func newOptions(opts ...Option) Options {
var o Options
for _, opt := range opts {
opt(&o)
}
if o.Expiry <= 0 {
o.Expiry = defaultExpiry
}
if o.NotFoundExpiry <= 0 {
o.NotFoundExpiry = defaultNotFoundExpiry
}
return o
}
4 years ago
// WithExpiry returns a func to customize a Options with given expiry.
4 years ago
func WithExpiry(expiry time.Duration) Option {
return func(o *Options) {
4 years ago
o.Expiry = expiry
}
}
// WithNotFoundExpiry returns a func to customize a Options with given not found expiry.
4 years ago
func WithNotFoundExpiry(expiry time.Duration) Option {
return func(o *Options) {
4 years ago
o.NotFoundExpiry = expiry
}
}