github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/framework/cache/2qcache.go (about) 1 package cache 2 3 import ( 4 lru "github.com/hashicorp/golang-lru" 5 "github.com/pkg/errors" 6 "github.com/unionj-cloud/go-doudou/toolkit/caller" 7 "time" 8 ) 9 10 type TwoQueueCache struct { 11 *base 12 recentRatio, ghostRatio float64 13 } 14 15 type TwoQueueCacheOption func(*TwoQueueCache) 16 17 func WithRecentRatio(recentRatio float64) TwoQueueCacheOption { 18 return func(tqc *TwoQueueCache) { 19 tqc.recentRatio = recentRatio 20 } 21 } 22 23 func WithGhostRatio(ghostRatio float64) TwoQueueCacheOption { 24 return func(tqc *TwoQueueCache) { 25 tqc.ghostRatio = ghostRatio 26 } 27 } 28 29 func NewTwoQueueCache(size int, ttl time.Duration, options ...TwoQueueCacheOption) *TwoQueueCache { 30 tqc := &TwoQueueCache{ 31 recentRatio: lru.Default2QRecentRatio, 32 ghostRatio: lru.Default2QGhostEntries, 33 } 34 for _, opt := range options { 35 opt(tqc) 36 } 37 store, err := lru.New2QParams(size, tqc.recentRatio, tqc.ghostRatio) 38 if err != nil { 39 panic(errors.Wrap(err, caller.NewCaller().String())) 40 } 41 tqc.base = newBase(store, ttl) 42 return tqc 43 }