aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/allegro/bigcache/config.go
blob: 9654143ab1329f72cca11dddc5d52e2eddf14558 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
}