|
| 1 | +package lfu |
| 2 | + |
| 3 | +import ( |
| 4 | + "container/heap" |
| 5 | + "sync" |
| 6 | + |
| 7 | + cache "github.com/Code-Hex/go-generics-cache" |
| 8 | +) |
| 9 | + |
| 10 | +// Cache is a thread safe LRU cache |
| 11 | +type Cache[K comparable, V any] struct { |
| 12 | + cap int |
| 13 | + queue *priorityQueue[K, V] |
| 14 | + items map[K]*entry[K, V] |
| 15 | + mu sync.RWMutex |
| 16 | +} |
| 17 | + |
| 18 | +var _ cache.Cache[interface{}, any] = (*Cache[interface{}, any])(nil) |
| 19 | + |
| 20 | +// NewCache creates a new LFU cache whose capacity is the default size (128). |
| 21 | +func NewCache[K comparable, V any]() *Cache[K, V] { |
| 22 | + return NewCacheWithCap[K, V](128) |
| 23 | +} |
| 24 | + |
| 25 | +// NewCacheWithCap creates a new LFU cache whose capacity is the specified size. |
| 26 | +func NewCacheWithCap[K comparable, V any](cap int) *Cache[K, V] { |
| 27 | + return &Cache[K, V]{ |
| 28 | + cap: cap, |
| 29 | + queue: newPriorityQueue[K, V](cap), |
| 30 | + items: make(map[K]*entry[K, V], cap), |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// Get looks up a key's value from the cache. |
| 35 | +func (c *Cache[K, V]) Get(key K) (zero V, _ bool) { |
| 36 | + c.mu.RLock() |
| 37 | + defer c.mu.RUnlock() |
| 38 | + |
| 39 | + e, ok := c.items[key] |
| 40 | + if !ok { |
| 41 | + return |
| 42 | + } |
| 43 | + if e.item.HasExpired() { |
| 44 | + return |
| 45 | + } |
| 46 | + e.item.Referenced() |
| 47 | + heap.Fix(c.queue, e.index) |
| 48 | + return e.item.Value, true |
| 49 | +} |
| 50 | + |
| 51 | +// Set sets a value to the cache with key. replacing any existing value. |
| 52 | +func (c *Cache[K, V]) Set(key K, val V, opts ...cache.ItemOption) { |
| 53 | + c.mu.Lock() |
| 54 | + defer c.mu.Unlock() |
| 55 | + |
| 56 | + if e, ok := c.items[key]; ok { |
| 57 | + c.queue.update(e, val) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + if len(c.items) == c.cap { |
| 62 | + evictedEntry := heap.Pop(c.queue).(*entry[K, V]) |
| 63 | + delete(c.items, evictedEntry.item.Key) |
| 64 | + } |
| 65 | + |
| 66 | + e := newEntry(key, val, opts...) |
| 67 | + heap.Push(c.queue, e) |
| 68 | + c.items[key] = e |
| 69 | +} |
| 70 | + |
| 71 | +// Keys returns the keys of the cache. the order is from oldest to newest. |
| 72 | +func (c *Cache[K, V]) Keys() []K { |
| 73 | + c.mu.RLock() |
| 74 | + defer c.mu.RUnlock() |
| 75 | + keys := make([]K, 0, len(c.items)) |
| 76 | + for _, entry := range *c.queue { |
| 77 | + keys = append(keys, entry.item.Key) |
| 78 | + } |
| 79 | + return keys |
| 80 | +} |
| 81 | + |
| 82 | +// Delete deletes the item with provided key from the cache. |
| 83 | +func (c *Cache[K, V]) Delete(key K) { |
| 84 | + c.mu.Lock() |
| 85 | + defer c.mu.Unlock() |
| 86 | + if e, ok := c.items[key]; ok { |
| 87 | + heap.Remove(c.queue, e.index) |
| 88 | + delete(c.items, key) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Contains reports whether key is within cache. |
| 93 | +func (c *Cache[K, V]) Contains(key K) bool { |
| 94 | + c.mu.RLock() |
| 95 | + defer c.mu.RUnlock() |
| 96 | + e, ok := c.items[key] |
| 97 | + if !ok { |
| 98 | + return false |
| 99 | + } |
| 100 | + return !e.item.HasExpired() |
| 101 | +} |
| 102 | + |
| 103 | +// Len returns the number of items in the cache. |
| 104 | +func (c *Cache[K, V]) Len() int { |
| 105 | + c.mu.RLock() |
| 106 | + defer c.mu.RUnlock() |
| 107 | + return c.queue.Len() |
| 108 | +} |
0 commit comments