-
Notifications
You must be signed in to change notification settings - Fork 351
/
eviction.go
58 lines (47 loc) · 1.46 KB
/
eviction.go
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
package pyramid
import (
"fmt"
"github.com/dgraph-io/ristretto"
"github.com/treeverse/lakefs/pkg/pyramid/params"
)
// nolint: unused
type ristrettoEviction struct {
cache *ristretto.Cache
evictCallback func(rPath params.RelativePath, cost int64)
}
const (
// 10M for an efficient 1M stored items (less than 5MB overhead)
numCounters = 10_000_000
// 64 is the recommended buffer-items for all use-cases
bufferItems = 64
)
// nolint: unused,deadcode
func newRistrettoEviction(capacity int64, evict func(rPath params.RelativePath, cost int64)) (params.Eviction, error) {
re := &ristrettoEviction{evictCallback: evict}
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: numCounters,
MaxCost: capacity,
BufferItems: bufferItems,
OnEvict: re.onEvict,
OnReject: re.onEvict,
})
if err != nil {
return nil, fmt.Errorf("creating ristretto cache: %w", err)
}
re.cache = cache
return re, nil
}
func (re *ristrettoEviction) Touch(rPath params.RelativePath) {
// update last access time, value is meaningless
re.cache.Get(string(rPath))
}
func (re *ristrettoEviction) Store(rPath params.RelativePath, filesize int64) bool {
// setting the path as the value since only the key hash is returned
// to the onEvict callback
return re.cache.Set(string(rPath), rPath, filesize)
}
func (re *ristrettoEviction) onEvict(item *ristretto.Item) {
if item.Value != nil {
re.evictCallback(item.Value.(params.RelativePath), item.Cost)
}
}