Skip to content

Commit

Permalink
Fix data race in Items()
Browse files Browse the repository at this point in the history
  • Loading branch information
hongkuancn authored and swithek committed Aug 24, 2024
1 parent 66d8e52 commit 2371d93
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,9 @@ func (c *Cache[K, V]) Items() map[K]*Item[K, V] {

items := make(map[K]*Item[K, V], len(c.items.values))
for k := range c.items.values {
item := c.get(k, false, false)
if item != nil {
items[k] = item.Value.(*Item[K, V])
item := c.items.values[k].Value.(*Item[K, V])
if item != nil && !item.isExpiredUnsafe() {
items[k] = item
}
}

Expand Down
1 change: 1 addition & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ func Test_Cache_Keys(t *testing.T) {

func Test_Cache_Items(t *testing.T) {
cache := prepCache(time.Hour, "1", "2", "3")
addToCache(cache, time.Nanosecond, "4")
items := cache.Items()
require.Len(t, items, 3)

Expand Down

0 comments on commit 2371d93

Please sign in to comment.