d41209d293
* Bump geth to 1.8.20 for Constantinople * Fix conflicting import/toml source for logrus
68 lines
2.6 KiB
Go
68 lines
2.6 KiB
Go
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. Default value is nil which means no callback and it prevents from unwrapping the oldest entry.
|
|
OnRemove func(key string, entry []byte)
|
|
|
|
// 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
|
|
}
|