Frames

https://github.com/allegro/bigcache/issues/148

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1package bigcache
2
3import "time"
4
5// Config for BigCache
6type Config struct {
7 // Number of cache shards, value must be a power of two
8 Shards int
9 // Time after which entry can be evicted
10 LifeWindow time.Duration
11 // Interval between removing expired entries (clean up).
12 // If set to <= 0 then no action is performed. Setting to < 1 second is counterproductive — bigcache has a one second resolution.
13 CleanWindow time.Duration
14 // Max number of entries in life window. Used only to calculate initial size for cache shards.
15 // When proper value is set then additional memory allocation does not occur.
16 MaxEntriesInWindow int
17 // Max size of entry in bytes. Used only to calculate initial size for cache shards.
18 MaxEntrySize int
19 // Verbose mode prints information about new memory allocation
20 Verbose bool
21 // Hasher used to map between string keys and unsigned 64bit integers, by default fnv64 hashing is used.
22 Hasher Hasher
23 // HardMaxCacheSize is a limit for cache size in MB. Cache will not allocate more memory than this limit.
24 // It can protect application from consuming all available memory on machine, therefore from running OOM Killer.
25 // Default value is 0 which means unlimited size. When the limit is higher than 0 and reached then
26 // the oldest entries are overridden for the new ones.
27 HardMaxCacheSize int5
28 // OnRemove is a callback fired when the oldest entry is removed because of its expiration time or no space left
29 // for the new entry, or because delete was called.
30 // Default value is nil which means no callback and it prevents from unwrapping the oldest entry.
31 OnRemove func(key string, entry []byte)
32 // OnRemoveWithReason is a callback fired when the oldest entry is removed because of its expiration time or no space left
33 // for the new entry, or because delete was called. A constant representing the reason will be passed through.
34 // Default value is nil which means no callback and it prevents from unwrapping the oldest entry.
35 // Ignored if OnRemove is specified.
36 OnRemoveWithReason func(key string, entry []byte, reason RemoveReason)
37
38 onRemoveFilter int
39
40 // Logger is a logging interface and used in combination with `Verbose`
41 // Defaults to `DefaultLogger()`
42 Logger Logger
43}
44
45// DefaultConfig initializes config with default values.
46// When load for BigCache can be predicted in advance then it is better to use custom config.
47func DefaultConfig(eviction time.Duration) Config {
48 return Config{
49 Shards: 1024,
50 LifeWindow: eviction,
51 CleanWindow: 1 * time.Second,
52 MaxEntriesInWindow: 1000 * 10 * 60,
53 MaxEntrySize: 500,
54 Verbose: true,
55 Hasher: newDefaultHasher(),
56 HardMaxCacheSize: 0,
57 Logger: DefaultLogger(),
58 }
59}
60
61// initialShardSize computes initial shard size
62func (c Config) initialShardSize() int {
63 return max(c.MaxEntriesInWindow/c.Shards, minimumEntriesInShard)
64}
65
66// maximumShardSize computes maximum shard size
67func (c Config) maximumShardSize() int {
68 maxShardSize := 0
69
70 if c.HardMaxCacheSize > 0 {
71 maxShardSize = convertMBToBytes(c.HardMaxCacheSize) / c.Shards
72 }
73
74 return maxShardSize
75}
76
77// OnRemoveFilterSet sets which remove reasons will trigger a call to OnRemoveWithReason.
78// Filtering out reasons prevents bigcache from unwrapping them, which saves cpu.
79func (c Config) OnRemoveFilterSet(reasons ...RemoveReason) Config {
80 c.onRemoveFilter = 0
81 for i := range reasons {
82 c.onRemoveFilter |= 1 << uint(reasons[i])
83 }
84
85 return c
86}
87