Skip to content

Commit

Permalink
Fixes race in getKeyMetadata accessor, when called from KeyMetadata (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cognusion authored Jul 6, 2020
1 parent 48f0a54 commit 21e5ca5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (c *BigCache) Stats() Stats {
func (c *BigCache) KeyMetadata(key string) Metadata {
hashedKey := c.hash.Sum64(key)
shard := c.getShard(hashedKey)
return shard.getKeyMetadata(hashedKey)
return shard.getKeyMetadataWithLock(hashedKey)
}

// Iterator returns iterator function to iterate over EntryInfo's from whole cache.
Expand Down
9 changes: 9 additions & 0 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ func (s *cacheShard) getStats() Stats {
return stats
}

func (s *cacheShard) getKeyMetadataWithLock(key uint64) Metadata {
s.lock.RLock()
c := s.hashmapStats[key]
s.lock.RUnlock()
return Metadata{
RequestCount: c,
}
}

func (s *cacheShard) getKeyMetadata(key uint64) Metadata {
return Metadata{
RequestCount: s.hashmapStats[key],
Expand Down

0 comments on commit 21e5ca5

Please sign in to comment.