Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
latitov committed Oct 28, 2022
1 parent b9e36a2 commit e850d7b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,19 @@ func (c *Cache[K, V]) Get(key K, opts ...Option[K, V]) *Item[K, V] {
}

func (c *Cache[K, V]) Transaction(f func(c *Cache[K, V])) {
lfoSaved := c.options.lockingFromOutside

if !c.options.lockingFromOutside {
c.options.lockingFromOutside = true // prevent locks from inside the transaction
c.CacheItems.Mu.Lock()
defer c.CacheItems.Mu.Unlock()
} else {
// expecting already been locked from outside
}

f(c)
if !c.options.lockingFromOutside {
c.CacheItems.Mu.Unlock()
}

c.options.lockingFromOutside = lfoSaved
}

// Delete deletes an item from the cache. If the item associated with
Expand Down

0 comments on commit e850d7b

Please sign in to comment.