github.com/lingyao2333/mo-zero@v1.4.1/core/stores/cache/cacheopt.go (about)

     1  package cache
     2  
     3  import "time"
     4  
     5  const (
     6  	defaultExpiry         = time.Hour * 24 * 7
     7  	defaultNotFoundExpiry = time.Minute
     8  )
     9  
    10  type (
    11  	// An Options is used to store the cache options.
    12  	Options struct {
    13  		Expiry         time.Duration
    14  		NotFoundExpiry time.Duration
    15  	}
    16  
    17  	// Option defines the method to customize an Options.
    18  	Option func(o *Options)
    19  )
    20  
    21  func newOptions(opts ...Option) Options {
    22  	var o Options
    23  	for _, opt := range opts {
    24  		opt(&o)
    25  	}
    26  
    27  	if o.Expiry <= 0 {
    28  		o.Expiry = defaultExpiry
    29  	}
    30  	if o.NotFoundExpiry <= 0 {
    31  		o.NotFoundExpiry = defaultNotFoundExpiry
    32  	}
    33  
    34  	return o
    35  }
    36  
    37  // WithExpiry returns a func to customize an Options with given expiry.
    38  func WithExpiry(expiry time.Duration) Option {
    39  	return func(o *Options) {
    40  		o.Expiry = expiry
    41  	}
    42  }
    43  
    44  // WithNotFoundExpiry returns a func to customize an Options with given not found expiry.
    45  func WithNotFoundExpiry(expiry time.Duration) Option {
    46  	return func(o *Options) {
    47  		o.NotFoundExpiry = expiry
    48  	}
    49  }