github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/cachekit/strategy_test.go (about) 1 // Copyright 2020 Insolar Network Ltd. 2 // All rights reserved. 3 // This material is licensed under the Insolar License version 1.0, 4 // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md. 5 6 package cachekit 7 8 import ( 9 "math" 10 ) 11 12 func newStrategy(pgSize, maxTotal int, useFence, trimEach bool) cacheStrategy { 13 return cacheStrategy{ 14 pgSize: pgSize, 15 maxTotal: maxTotal, 16 useFence: useFence, 17 trimEach: trimEach, 18 } 19 } 20 21 var _ Strategy = cacheStrategy{} 22 type cacheStrategy struct { 23 pgSize, maxTotal int 24 useFence bool 25 trimEach bool 26 } 27 28 func (v cacheStrategy) TrimOnEachAddition() bool { 29 return v.trimEach 30 } 31 32 func (v cacheStrategy) CurrentAge() Age { 33 return 0 34 } 35 36 func (v cacheStrategy) AllocationPageSize() int { 37 return v.pgSize 38 } 39 40 func (v cacheStrategy) InitGenerationCapacity() (pageSize int, useFence bool) { 41 return v.pgSize, v.useFence 42 } 43 44 func (v cacheStrategy) NextGenerationCapacity(prevLen int, prevCap int) (int, bool) { 45 return v.pgSize, v.useFence 46 } 47 48 func (v cacheStrategy) CanTrimGenerations(totalCount, freqGenCount int, recent, rarest, oldest Age) int { 49 n := (v.maxTotal / v.pgSize) >> 2 50 if n < 10 { 51 n = 10 52 } 53 return freqGenCount - n 54 } 55 56 func (v cacheStrategy) CanTrimEntries(totalCount int, recent, oldest Age) int { 57 return totalCount - v.maxTotal 58 } 59 60 func (v cacheStrategy) CanAdvanceGeneration(curLen int, curCap int, hitRemains uint64, start, end Age) (createGeneration bool, hitLimit uint64, ageLimit Age) { 61 return false, math.MaxUint64, math.MaxInt64 62 } 63 64 func (v cacheStrategy) InitialAdvanceLimits(curCap int, start Age) (hitLimit uint64, ageLimit Age) { 65 return math.MaxUint64, math.MaxInt64 66 } 67