|
| 1 | +package p2p |
| 2 | + |
| 3 | +import ( |
| 4 | + "container/list" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// Cache is a thread-safe LRU cache with optional TTL-based expiration. |
| 10 | +type Cache[K comparable, V any] struct { |
| 11 | + mu sync.RWMutex |
| 12 | + maxSize int |
| 13 | + ttl time.Duration |
| 14 | + items map[K]*list.Element |
| 15 | + list *list.List |
| 16 | +} |
| 17 | + |
| 18 | +type entry[K comparable, V any] struct { |
| 19 | + key K |
| 20 | + value V |
| 21 | + expiresAt time.Time |
| 22 | +} |
| 23 | + |
| 24 | +// NewCache creates a new cache with the given max size and optional TTL. |
| 25 | +// If maxSize <= 0, the cache has no size limit. |
| 26 | +// If ttl is 0, entries never expire based on time. |
| 27 | +func NewCache[K comparable, V any](maxSize int, ttl time.Duration) *Cache[K, V] { |
| 28 | + return &Cache[K, V]{ |
| 29 | + maxSize: maxSize, |
| 30 | + ttl: ttl, |
| 31 | + items: make(map[K]*list.Element), |
| 32 | + list: list.New(), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Add adds or updates a value in the cache. |
| 37 | +func (c *Cache[K, V]) Add(key K, value V) { |
| 38 | + c.mu.Lock() |
| 39 | + defer c.mu.Unlock() |
| 40 | + |
| 41 | + now := time.Now() |
| 42 | + expiresAt := time.Time{} |
| 43 | + if c.ttl > 0 { |
| 44 | + expiresAt = now.Add(c.ttl) |
| 45 | + } |
| 46 | + |
| 47 | + if elem, ok := c.items[key]; ok { |
| 48 | + c.list.MoveToFront(elem) |
| 49 | + e := elem.Value.(*entry[K, V]) |
| 50 | + e.value = value |
| 51 | + e.expiresAt = expiresAt |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + e := &entry[K, V]{ |
| 56 | + key: key, |
| 57 | + value: value, |
| 58 | + expiresAt: expiresAt, |
| 59 | + } |
| 60 | + elem := c.list.PushFront(e) |
| 61 | + c.items[key] = elem |
| 62 | + |
| 63 | + if c.maxSize > 0 && c.list.Len() > c.maxSize { |
| 64 | + back := c.list.Back() |
| 65 | + if back != nil { |
| 66 | + c.list.Remove(back) |
| 67 | + e := back.Value.(*entry[K, V]) |
| 68 | + delete(c.items, e.key) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Get retrieves a value from the cache and updates LRU ordering. |
| 74 | +func (c *Cache[K, V]) Get(key K) (V, bool) { |
| 75 | + c.mu.Lock() |
| 76 | + defer c.mu.Unlock() |
| 77 | + |
| 78 | + elem, ok := c.items[key] |
| 79 | + if !ok { |
| 80 | + var zero V |
| 81 | + return zero, false |
| 82 | + } |
| 83 | + |
| 84 | + e := elem.Value.(*entry[K, V]) |
| 85 | + |
| 86 | + if c.ttl > 0 && time.Now().After(e.expiresAt) { |
| 87 | + c.list.Remove(elem) |
| 88 | + delete(c.items, key) |
| 89 | + var zero V |
| 90 | + return zero, false |
| 91 | + } |
| 92 | + |
| 93 | + c.list.MoveToFront(elem) |
| 94 | + return e.value, true |
| 95 | +} |
| 96 | + |
| 97 | +// Contains checks if a key exists in the cache and is not expired. |
| 98 | +// Uses a read lock and doesn't update LRU ordering. |
| 99 | +func (c *Cache[K, V]) Contains(key K) bool { |
| 100 | + c.mu.RLock() |
| 101 | + defer c.mu.RUnlock() |
| 102 | + |
| 103 | + elem, ok := c.items[key] |
| 104 | + if !ok { |
| 105 | + return false |
| 106 | + } |
| 107 | + |
| 108 | + e := elem.Value.(*entry[K, V]) |
| 109 | + |
| 110 | + if c.ttl > 0 && time.Now().After(e.expiresAt) { |
| 111 | + return false |
| 112 | + } |
| 113 | + |
| 114 | + return true |
| 115 | +} |
| 116 | + |
| 117 | +// Remove removes a key from the cache. |
| 118 | +func (c *Cache[K, V]) Remove(key K) { |
| 119 | + c.mu.Lock() |
| 120 | + defer c.mu.Unlock() |
| 121 | + |
| 122 | + if elem, ok := c.items[key]; ok { |
| 123 | + c.list.Remove(elem) |
| 124 | + delete(c.items, key) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// Len returns the number of items in the cache. |
| 129 | +func (c *Cache[K, V]) Len() int { |
| 130 | + c.mu.RLock() |
| 131 | + defer c.mu.RUnlock() |
| 132 | + return c.list.Len() |
| 133 | +} |
| 134 | + |
| 135 | +// Purge clears all items from the cache. |
| 136 | +func (c *Cache[K, V]) Purge() { |
| 137 | + c.mu.Lock() |
| 138 | + defer c.mu.Unlock() |
| 139 | + |
| 140 | + c.items = make(map[K]*list.Element) |
| 141 | + c.list.Init() |
| 142 | +} |
| 143 | + |
| 144 | +// Keys returns all keys in the cache. |
| 145 | +func (c *Cache[K, V]) Keys() []K { |
| 146 | + c.mu.RLock() |
| 147 | + defer c.mu.RUnlock() |
| 148 | + |
| 149 | + keys := make([]K, 0, c.list.Len()) |
| 150 | + for elem := c.list.Front(); elem != nil; elem = elem.Next() { |
| 151 | + e := elem.Value.(*entry[K, V]) |
| 152 | + keys = append(keys, e.key) |
| 153 | + } |
| 154 | + return keys |
| 155 | +} |
0 commit comments