Skip to content

Commit 21e5ca5

Browse files
authored
Fixes race in getKeyMetadata accessor, when called from KeyMetadata (#233)
1 parent 48f0a54 commit 21e5ca5

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

bigcache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (c *BigCache) Stats() Stats {
193193
func (c *BigCache) KeyMetadata(key string) Metadata {
194194
hashedKey := c.hash.Sum64(key)
195195
shard := c.getShard(hashedKey)
196-
return shard.getKeyMetadata(hashedKey)
196+
return shard.getKeyMetadataWithLock(hashedKey)
197197
}
198198

199199
// Iterator returns iterator function to iterate over EntryInfo's from whole cache.

shard.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,15 @@ func (s *cacheShard) getStats() Stats {
341341
return stats
342342
}
343343

344+
func (s *cacheShard) getKeyMetadataWithLock(key uint64) Metadata {
345+
s.lock.RLock()
346+
c := s.hashmapStats[key]
347+
s.lock.RUnlock()
348+
return Metadata{
349+
RequestCount: c,
350+
}
351+
}
352+
344353
func (s *cacheShard) getKeyMetadata(key uint64) Metadata {
345354
return Metadata{
346355
RequestCount: s.hashmapStats[key],

0 commit comments

Comments
 (0)