Skip to content

Commit 138ae89

Browse files
authored
fix(w-tinylfu): missing inc (#58)
1 parent cb01c6c commit 138ae89

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

pkg/wtinylfu/wtinylfu.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ var _ base.InMemoryCache[string, int] = (*WTinyLFUCache[string, int])(nil)
9292

9393
// Set stores a key-value pair in the cache.
9494
func (c *WTinyLFUCache[K, V]) Set(key K, value V) {
95+
// Increment frequency exactly ONCE per cache access (W-TinyLFU paper spec)
96+
c.sketch.Inc(key)
97+
9598
// Check if key exists in protected segment
9699
if e, ok := c.protectedMap[key]; ok {
97100
c.protectedLl.MoveToFront(e)
@@ -381,7 +384,7 @@ func (c *WTinyLFUCache[K, V]) promoteToProtected(e *list.Element[*entry[K, V]])
381384
} else {
382385
// Protected is full, compete with least recently used protected item
383386
protectedVictim := c.protectedLl.Back()
384-
if protectedVictim != nil && entry.freq > protectedVictim.Value.freq {
387+
if protectedVictim != nil && c.sketch.Estimate(entry.key) > c.sketch.Estimate(protectedVictim.Value.key) {
385388
// Evict protected victim, promote this item
386389
c.evictFromProtected()
387390
newElement := c.protectedLl.PushFront(entry)

0 commit comments

Comments
 (0)