cacheopt.go 959 B

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