vitess.io/vitess@v0.16.2/go/cache/ristretto.go (about)

     1  package cache
     2  
     3  import (
     4  	"vitess.io/vitess/go/cache/ristretto"
     5  )
     6  
     7  var _ Cache = &ristretto.Cache{}
     8  
     9  // NewRistrettoCache returns a Cache implementation based on Ristretto
    10  func NewRistrettoCache(maxEntries, maxCost int64, cost func(any) int64) *ristretto.Cache {
    11  	// The TinyLFU paper recommends to allocate 10x times the max entries amount as counters
    12  	// for the admission policy; since our caches are small and we're very interested on admission
    13  	// accuracy, we're a bit more greedy than 10x
    14  	const CounterRatio = 12
    15  
    16  	config := ristretto.Config{
    17  		NumCounters: maxEntries * CounterRatio,
    18  		MaxCost:     maxCost,
    19  		BufferItems: 64,
    20  		Metrics:     true,
    21  		Cost:        cost,
    22  	}
    23  	cache, err := ristretto.NewCache(&config)
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  	return cache
    28  }