mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
3.4 KiB
87 lines
3.4 KiB
6 years ago
|
package bigcache
|
||
|
|
||
|
import "time"
|
||
|
|
||
|
// Config for BigCache
|
||
|
type Config struct {
|
||
|
// Number of cache shards, value must be a power of two
|
||
|
Shards int
|
||
|
// Time after which entry can be evicted
|
||
|
LifeWindow time.Duration
|
||
|
// Interval between removing expired entries (clean up).
|
||
|
// If set to <= 0 then no action is performed. Setting to < 1 second is counterproductive — bigcache has a one second resolution.
|
||
|
CleanWindow time.Duration
|
||
|
// Max number of entries in life window. Used only to calculate initial size for cache shards.
|
||
|
// When proper value is set then additional memory allocation does not occur.
|
||
|
MaxEntriesInWindow int
|
||
|
// Max size of entry in bytes. Used only to calculate initial size for cache shards.
|
||
|
MaxEntrySize int
|
||
|
// Verbose mode prints information about new memory allocation
|
||
|
Verbose bool
|
||
|
// Hasher used to map between string keys and unsigned 64bit integers, by default fnv64 hashing is used.
|
||
|
Hasher Hasher
|
||
|
// HardMaxCacheSize is a limit for cache size in MB. Cache will not allocate more memory than this limit.
|
||
|
// It can protect application from consuming all available memory on machine, therefore from running OOM Killer.
|
||
|
// Default value is 0 which means unlimited size. When the limit is higher than 0 and reached then
|
||
|
// the oldest entries are overridden for the new ones.
|
||
|
HardMaxCacheSize int
|
||
|
// OnRemove is a callback fired when the oldest entry is removed because of its expiration time or no space left
|
||
|
// for the new entry, or because delete was called.
|
||
|
// Default value is nil which means no callback and it prevents from unwrapping the oldest entry.
|
||
|
OnRemove func(key string, entry []byte)
|
||
|
// OnRemoveWithReason is a callback fired when the oldest entry is removed because of its expiration time or no space left
|
||
|
// for the new entry, or because delete was called. A constant representing the reason will be passed through.
|
||
|
// Default value is nil which means no callback and it prevents from unwrapping the oldest entry.
|
||
|
// Ignored if OnRemove is specified.
|
||
|
OnRemoveWithReason func(key string, entry []byte, reason RemoveReason)
|
||
|
|
||
|
onRemoveFilter int
|
||
|
|
||
|
// Logger is a logging interface and used in combination with `Verbose`
|
||
|
// Defaults to `DefaultLogger()`
|
||
|
Logger Logger
|
||
|
}
|
||
|
|
||
|
// DefaultConfig initializes config with default values.
|
||
|
// When load for BigCache can be predicted in advance then it is better to use custom config.
|
||
|
func DefaultConfig(eviction time.Duration) Config {
|
||
|
return Config{
|
||
|
Shards: 1024,
|
||
|
LifeWindow: eviction,
|
||
|
CleanWindow: 0,
|
||
|
MaxEntriesInWindow: 1000 * 10 * 60,
|
||
|
MaxEntrySize: 500,
|
||
|
Verbose: true,
|
||
|
Hasher: newDefaultHasher(),
|
||
|
HardMaxCacheSize: 0,
|
||
|
Logger: DefaultLogger(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// initialShardSize computes initial shard size
|
||
|
func (c Config) initialShardSize() int {
|
||
|
return max(c.MaxEntriesInWindow/c.Shards, minimumEntriesInShard)
|
||
|
}
|
||
|
|
||
|
// maximumShardSize computes maximum shard size
|
||
|
func (c Config) maximumShardSize() int {
|
||
|
maxShardSize := 0
|
||
|
|
||
|
if c.HardMaxCacheSize > 0 {
|
||
|
maxShardSize = convertMBToBytes(c.HardMaxCacheSize) / c.Shards
|
||
|
}
|
||
|
|
||
|
return maxShardSize
|
||
|
}
|
||
|
|
||
|
// OnRemoveFilterSet sets which remove reasons will trigger a call to OnRemoveWithReason.
|
||
|
// Filtering out reasons prevents bigcache from unwrapping them, which saves cpu.
|
||
|
func (c Config) OnRemoveFilterSet(reasons ...RemoveReason) Config {
|
||
|
c.onRemoveFilter = 0
|
||
|
for i := range reasons {
|
||
|
c.onRemoveFilter |= 1 << uint(reasons[i])
|
||
|
}
|
||
|
|
||
|
return c
|
||
|
}
|