Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ func (c *BigCache) Reset() error {
return nil
}

// ResetStats resets cache stats
func (c *BigCache) ResetStats() error {
for _, shard := range c.shards {
shard.resetStats()
}
return nil
}

// Len computes number of entries in cache
func (c *BigCache) Len() int {
var len int
Expand Down
49 changes: 49 additions & 0 deletions bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,55 @@ func TestCacheEntryStats(t *testing.T) {
assertEqual(t, uint32(10), keyMetadata.RequestCount)
}

func TestCacheRestStats(t *testing.T) {
t.Parallel()

// given
cache, _ := NewBigCache(Config{
Shards: 8,
LifeWindow: time.Second,
MaxEntriesInWindow: 1,
MaxEntrySize: 256,
})

// when
for i := 0; i < 100; i++ {
cache.Set(fmt.Sprintf("key%d", i), []byte("value"))
}

for i := 0; i < 10; i++ {
value, err := cache.Get(fmt.Sprintf("key%d", i))
noError(t, err)
assertEqual(t, string(value), "value")
}
for i := 100; i < 110; i++ {
_, err := cache.Get(fmt.Sprintf("key%d", i))
assertEqual(t, ErrEntryNotFound, err)
}
for i := 10; i < 20; i++ {
err := cache.Delete(fmt.Sprintf("key%d", i))
noError(t, err)
}
for i := 110; i < 120; i++ {
err := cache.Delete(fmt.Sprintf("key%d", i))
assertEqual(t, ErrEntryNotFound, err)
}

stats := cache.Stats()
assertEqual(t, stats.Hits, int64(10))
assertEqual(t, stats.Misses, int64(10))
assertEqual(t, stats.DelHits, int64(10))
assertEqual(t, stats.DelMisses, int64(10))

//then
cache.ResetStats()
stats = cache.Stats()
assertEqual(t, stats.Hits, int64(0))
assertEqual(t, stats.Misses, int64(0))
assertEqual(t, stats.DelHits, int64(0))
assertEqual(t, stats.DelMisses, int64(0))
}

func TestCacheDel(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 6 additions & 0 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ func (s *cacheShard) reset(config Config) {
s.lock.Unlock()
}

func (s *cacheShard) resetStats() {
s.lock.Lock()
s.stats = Stats{}
s.lock.Unlock()
}

func (s *cacheShard) len() int {
s.lock.RLock()
res := len(s.hashmap)
Expand Down