Skip to content

Commit

Permalink
Fix Range and RangeBackwards mutex usage (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
iurii-ssv authored Jan 18, 2025
1 parent 1ae4056 commit e25850a
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,16 +558,14 @@ func (c *Cache[K, V]) Range(fn func(item *Item[K, V]) bool) {
for item := c.items.lru.Front(); item != c.items.lru.Back().Next(); item = item.Next() {
i := item.Value.(*Item[K, V])
expired := i.isExpiredUnsafe()
c.items.mu.RUnlock()

c.items.mu.RUnlock() // unlock mutex so fn func can access it (if it needs to)
if !expired && !fn(i) {
return
}

if item.Next() != nil {
c.items.mu.RLock()
}
c.items.mu.RLock()
}

c.items.mu.RUnlock()
}

// RangeBackwards calls fn for each unexpired item in the cache in reverse order.
Expand All @@ -584,16 +582,14 @@ func (c *Cache[K, V]) RangeBackwards(fn func(item *Item[K, V]) bool) {
for item := c.items.lru.Back(); item != c.items.lru.Front().Prev(); item = item.Prev() {
i := item.Value.(*Item[K, V])
expired := i.isExpiredUnsafe()
c.items.mu.RUnlock()

c.items.mu.RUnlock() // unlock mutex so fn func can access it (if it needs to)
if !expired && !fn(i) {
return
}

if item.Prev() != nil {
c.items.mu.RLock()
}
c.items.mu.RLock()
}

c.items.mu.RUnlock()
}

// Metrics returns the metrics of the cache.
Expand Down

0 comments on commit e25850a

Please sign in to comment.