Skip to content

Commit

Permalink
Clarify map backend usage
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Apr 12, 2024
1 parent fd1c572 commit 74a85af
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ For a more detailed guide, see [reference](https://pkg.go.dev/github.com/motoki3
## Supported cache backends (cache replacement policy)

- Built-in map (default)
- Note: This backend cannot have max number of items configured. It holds all values in memory until expiration.
- Note: This backend cannot have max number of items configured. It holds all values in memory until expiration. For more, see the [documentation](https://pkg.go.dev/github.com/motoki317/sc#WithMapBackend).
- LRU (Least Recently Used)
- 2Q (Two Queue Cache)

Expand Down
12 changes: 9 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ func defaultConfig(ttl time.Duration) cacheConfig {
}

// WithMapBackend specifies to use the built-in map for storing cache items (the default).
// Note that the default map backend will not evict old cache items. If your key's cardinality is high, consider using
// other backends such as LRU.
//
// Note that this default map backend cannot have the maximum number of items configured,
// so it holds all values in memory until expired values are cleaned regularly
// at the interval specified by WithCleanupInterval.
//
// If your key's cardinality is high and if you would like to hard-limit the cache's memory usage,
// consider using other backends such as LRU backend.
//
// Initial capacity needs to be non-negative.
func WithMapBackend(initialCapacity int) CacheOption {
return func(c *cacheConfig) {
Expand Down Expand Up @@ -138,7 +144,7 @@ func EnableStrictCoalescing() CacheOption {
// WithCleanupInterval specifies cleanup interval of expired items.
//
// 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),
// This means if you use 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.
//
Expand Down

0 comments on commit 74a85af

Please sign in to comment.