-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheviction.go
More file actions
87 lines (65 loc) · 2.3 KB
/
Copy patheviction.go
File metadata and controls
87 lines (65 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package tempuscache
import "container/list"
/*
evictOldest removes the least recently used (LRU) entry
from the cache when capacity constraints are exceeded.
================================================================================
EVICTION POLICY
================================================================================
TempusCache uses a strict LRU (Least Recently Used) policy:
- Most recently accessed entries are moved to the front.
- Least recently used entries remain at the back.
- When maxEntries is reached, the oldest entry is evicted.
This guarantees predictable memory bounds and deterministic
eviction behavior.
================================================================================
ALGORITHM
================================================================================
1. Retrieve the last element from the LRU list.
2. If it exists:
- Remove it from both:
a) The linked list
b) The hash map
- Increment eviction statistics counter.
TIME COMPLEXITY:
O(1)
The use of a doubly linked list ensures constant-time removal.
*/
func (c *Cache) evictOldest() {
elem := c.lru.Back()
if elem != nil {
c.removeElement(elem)
c.stats.Evictions++
}
}
/*
removeElement removes a given list element from both
the LRU list and the primary storage map.
================================================================================
RESPONSIBILITY
================================================================================
This is an internal helper method used by:
- LRU eviction
- Lazy expiration
- Active expiration (janitor)
- Explicit delete (if implemented using it)
================================================================================
CONSISTENCY GUARANTEE
================================================================================
To maintain structural integrity:
- The element is first removed from the linked list.
- The corresponding key is then deleted from the map.
This ensures there are no dangling references between
the list and the hash map.
TIME COMPLEXITY:
O(1)
NOTE:
This function assumes the caller already holds
the appropriate lock (Lock or RLock upgrade scenario).
It does NOT perform its own synchronization.
*/
func (c *Cache) removeElement(e *list.Element) {
c.lru.Remove(e)
item := e.Value.(*Item)
delete(c.data, item.key)
}