From 21e5ca5c3d539f94e8dc563350acd97c5400154f Mon Sep 17 00:00:00 2001 From: "M@" Date: Mon, 6 Jul 2020 15:36:52 -0400 Subject: [PATCH] Fixes race in getKeyMetadata accessor, when called from KeyMetadata (#233) --- bigcache.go | 2 +- shard.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/bigcache.go b/bigcache.go index d5de8144..b59eba08 100644 --- a/bigcache.go +++ b/bigcache.go @@ -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. diff --git a/shard.go b/shard.go index a7e9f119..a11e7060 100644 --- a/shard.go +++ b/shard.go @@ -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],