Repository: allegro/bigcache
Pull request #331 (2022-09-06 09:09:48)

Add ResetStats
Hello, I saw the following issues #322 , #315 .
So I wrote the code.

codecov-commenter: # [Codecov](https://codecov.io/gh/allegro/bigcache/pull/331?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#331](https://codecov.io/gh/allegro/bigcache/pull/331?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (ff1c855) into [master](https://codecov.io/gh/allegro/bigcache/commit/0d61bd727b03b7ad185df9d013fe7e6cb2a65ed3?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (0d61bd7) will **decrease** coverage by `0.40%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/allegro/bigcache/pull/331/graphs/tree.svg?width=650&height=150&src=pr&token=NRx0gXD6he&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)](https://codecov.io/gh/allegro/bigcache/pull/331?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

```diff
@@            Coverage Diff             @@
##           master     #331      +/-   ##
==========================================
- Coverage   90.07%   89.66%   -0.41%     
==========================================
  Files          15       15              
  Lines         776      784       +8     
==========================================
+ Hits          699      703       +4     
- Misses         65       68       +3     
- Partials       12       13       +1     
```


| [Impacted Files](https://codecov.io/gh/allegro/bigcache/pull/331?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [bigcache.go](https://codecov.io/gh/allegro/bigcache/pull/331/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-YmlnY2FjaGUuZ28=) | `96.74% <100.00%> (+0.10%)` | :arrow_up: |
| [shard.go](https://codecov.io/gh/allegro/bigcache/pull/331/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c2hhcmQuZ28=) | `90.49% <100.00%> (-1.40%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/allegro/bigcache/pull/331?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/allegro/bigcache/pull/331?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Last update [0d61bd7...ff1c855](https://codecov.io/gh/allegro/bigcache/pull/331?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).


Patch:
diff --git a/bigcache.go b/bigcache.go
index 393753e..0a09b5e 100644
--- a/bigcache.go
+++ b/bigcache.go
@@ -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
diff --git a/shard.go b/shard.go
index af718b4..e80a759 100644
--- a/shard.go
+++ b/shard.go
@@ -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)


Test patch:
diff --git a/bigcache_test.go b/bigcache_test.go
index 0f66626..2323e5b 100644
--- a/bigcache_test.go
+++ b/bigcache_test.go
@@ -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()