cache_config.go 477 B

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