|
| 1 | +// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package cache |
| 5 | + |
| 6 | +import ( |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/ava-labs/avalanchego/utils" |
| 10 | + "github.com/ava-labs/avalanchego/utils/linkedhashmap" |
| 11 | +) |
| 12 | + |
| 13 | +var _ Cacher[struct{}, SizedElement] = (*sizedLRU[struct{}, SizedElement])(nil) |
| 14 | + |
| 15 | +type SizedElement interface { |
| 16 | + Size() int |
| 17 | +} |
| 18 | + |
| 19 | +// sizedLRU is a key value store with bounded size. If the size is attempted to |
| 20 | +// be exceeded, then elements are removed from the cache until the bound is |
| 21 | +// honored, based on evicting the least recently used value. |
| 22 | +type sizedLRU[K comparable, V SizedElement] struct { |
| 23 | + lock sync.Mutex |
| 24 | + elements linkedhashmap.LinkedHashmap[K, V] |
| 25 | + maxSize int |
| 26 | + currentSize int |
| 27 | +} |
| 28 | + |
| 29 | +func NewSizedLRU[K comparable, V SizedElement](maxSize int) Cacher[K, V] { |
| 30 | + return &sizedLRU[K, V]{ |
| 31 | + elements: linkedhashmap.New[K, V](), |
| 32 | + maxSize: maxSize, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (c *sizedLRU[K, V]) Put(key K, value V) { |
| 37 | + c.lock.Lock() |
| 38 | + defer c.lock.Unlock() |
| 39 | + |
| 40 | + c.put(key, value) |
| 41 | +} |
| 42 | + |
| 43 | +func (c *sizedLRU[K, V]) Get(key K) (V, bool) { |
| 44 | + c.lock.Lock() |
| 45 | + defer c.lock.Unlock() |
| 46 | + |
| 47 | + return c.get(key) |
| 48 | +} |
| 49 | + |
| 50 | +func (c *sizedLRU[K, V]) Evict(key K) { |
| 51 | + c.lock.Lock() |
| 52 | + defer c.lock.Unlock() |
| 53 | + |
| 54 | + c.evict(key) |
| 55 | +} |
| 56 | + |
| 57 | +func (c *sizedLRU[K, V]) Flush() { |
| 58 | + c.lock.Lock() |
| 59 | + defer c.lock.Unlock() |
| 60 | + |
| 61 | + c.flush() |
| 62 | +} |
| 63 | + |
| 64 | +func (c *sizedLRU[_, _]) PortionFilled() float64 { |
| 65 | + c.lock.Lock() |
| 66 | + defer c.lock.Unlock() |
| 67 | + |
| 68 | + return c.portionFilled() |
| 69 | +} |
| 70 | + |
| 71 | +func (c *sizedLRU[K, V]) put(key K, value V) { |
| 72 | + valueSize := value.Size() |
| 73 | + if valueSize > c.maxSize { |
| 74 | + c.flush() |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + if oldValue, ok := c.elements.Get(key); ok { |
| 79 | + c.currentSize -= oldValue.Size() |
| 80 | + } |
| 81 | + |
| 82 | + // Remove elements until the size of elements in the cache <= [c.maxSize]. |
| 83 | + for c.currentSize > c.maxSize-valueSize { |
| 84 | + oldestKey, value, _ := c.elements.Oldest() |
| 85 | + c.elements.Delete(oldestKey) |
| 86 | + c.currentSize -= value.Size() |
| 87 | + } |
| 88 | + |
| 89 | + c.elements.Put(key, value) |
| 90 | + c.currentSize += valueSize |
| 91 | +} |
| 92 | + |
| 93 | +func (c *sizedLRU[K, V]) get(key K) (V, bool) { |
| 94 | + value, ok := c.elements.Get(key) |
| 95 | + if !ok { |
| 96 | + return utils.Zero[V](), false |
| 97 | + } |
| 98 | + |
| 99 | + c.elements.Put(key, value) // Mark [k] as MRU. |
| 100 | + return value, true |
| 101 | +} |
| 102 | + |
| 103 | +func (c *sizedLRU[K, _]) evict(key K) { |
| 104 | + if value, ok := c.elements.Get(key); ok { |
| 105 | + c.elements.Delete(key) |
| 106 | + c.currentSize -= value.Size() |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func (c *sizedLRU[K, V]) flush() { |
| 111 | + c.elements = linkedhashmap.New[K, V]() |
| 112 | + c.currentSize = 0 |
| 113 | +} |
| 114 | + |
| 115 | +func (c *sizedLRU[_, _]) portionFilled() float64 { |
| 116 | + return float64(c.currentSize) / float64(c.maxSize) |
| 117 | +} |
0 commit comments