Skip to content

Commit

Permalink
Clean up default options
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnsk committed Aug 9, 2024
1 parent 23eab9e commit e4d66b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
4 changes: 1 addition & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ type Cache[K comparable, V any] struct {

// New creates a new empty cache.
func New[K comparable, V any](opt ...Option) *Cache[K, V] {
opts := cacheOptions{
debounce: 1 * time.Second,
}
opts := newDefaultCacheOptions()

for _, o := range opt {
o.apply(&opts)
Expand Down
22 changes: 9 additions & 13 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ type cacheOptions struct {
debounce time.Duration
}

func newDefaultCacheOptions() cacheOptions {
return cacheOptions{
policy: FIFO,
capacity: 0,
ttl: 0,
debounce: 1 * time.Second,
}
}

// WithCapacity option configures the cache with specified capacity.
//
// The zero values configures unbounded capacity.
Expand Down Expand Up @@ -64,19 +73,6 @@ func WithTTL(ttl time.Duration) Option {
})
}

// WithExpiryDebounce returns an option that configures the cache with specified expiry eviction debounce duration.
//
// The zero value configures the default 1s debounce duration.
func WithExpiryDebounce(debounce time.Duration) Option {
return funcOption(func(opts *cacheOptions) {
if debounce == 0 {
opts.debounce = 1 * time.Second
} else {
opts.debounce = debounce
}
})
}

type funcOption func(*cacheOptions)

func (o funcOption) apply(opts *cacheOptions) {
Expand Down

0 comments on commit e4d66b3

Please sign in to comment.