cache_config.go 656 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package ttl_memory_cache
  2. import "time"
  3. type Config struct {
  4. expiration *time.Duration
  5. cleanupInterval *time.Duration
  6. }
  7. type ConfigOption func(*Config)
  8. type ConfigOptions []ConfigOption
  9. func (s *ConfigOptions) Add(option ConfigOption) {
  10. *s = append(*s, option)
  11. }
  12. func NewConfig(opts ...ConfigOption) Config {
  13. c := &Config{}
  14. for _, opt := range opts {
  15. opt(c)
  16. }
  17. return *c
  18. }
  19. func WithExpiration(expiration time.Duration) ConfigOption {
  20. return func(s *Config) {
  21. s.expiration = &expiration
  22. }
  23. }
  24. func WithCleanupInterval(cleanupInterval time.Duration) ConfigOption {
  25. return func(s *Config) {
  26. s.cleanupInterval = &cleanupInterval
  27. }
  28. }