Skip to content

Commit

Permalink
Set cleanup interval by default resolves #2
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Nov 5, 2023
1 parent 27d0aa3 commit fd7bc93
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ For a more detailed guide, see [reference](https://pkg.go.dev/github.com/motoki3

## Supported cache backends (cache replacement policy)

The default backend is the built-in map.
This is ultra-lightweight, but does **not** evict items.
You should only use the built-in map backend if your key's cardinality is finite,
and you are comfortable holding **all** values in-memory.

Otherwise, you should use LRU or 2Q backend which automatically evicts overflown items.

- Built-in map (default)
- LRU (Least Recently Used)
- 2Q (Two Queue Cache)
Expand Down
2 changes: 1 addition & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func New[K comparable, V any](replaceFn replaceFunc[K, V], freshFor, ttl time.Du
return nil, errors.New("freshFor cannot be longer than ttl")
}

config := defaultConfig()
config := defaultConfig(ttl)
for _, option := range options {
option(&config)
}
Expand Down
17 changes: 13 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ const (
cacheBackend2Q
)

func defaultConfig() cacheConfig {
func defaultConfig(ttl time.Duration) cacheConfig {
cleanupInterval := 2 * ttl
if ttl == 0 {
cleanupInterval = 60 * time.Second
}
return cacheConfig{
enableStrictCoalescing: false,
backend: cacheBackendMap,
capacity: 0,
cleanupInterval: 0,
cleanupInterval: cleanupInterval,
}
}

Expand Down Expand Up @@ -80,8 +84,13 @@ func EnableStrictCoalescing() CacheOption {

// WithCleanupInterval specifies cleanup interval of expired items.
//
// Note that by default, a cache will be initialized without a cleaner.
// Try tuning your cache size (and using non-map backend) before using this option.
// Setting interval of 0 (or negative) will disable the cleaner.
// This means if using non-evicting cache backend (that is, the default, built-in map backend),
// the cache keeps holding key-value pairs indefinitely.
// If cardinality of key is very large, this leads to memory leak.
//
// By default, a cleaner runs every once in 2x ttl (or every 60s if ttl == 0).
// Try tuning your cache size (and using non-map backend) before tuning this option.
// Using cleanup interval on a cache with many items may decrease the through-put,
// since the cleaner has to acquire the lock to iterate through all items.
func WithCleanupInterval(interval time.Duration) CacheOption {
Expand Down

0 comments on commit fd7bc93

Please sign in to comment.