bigcache
bigcache copied to clipboard
Reset() don't work
What is the issue you are having? Reset shards no longer works
What is BigCache doing that it shouldn't? Empty the shards.
Using stats method i can see the how many items are in cache
cache, initErr := bigcache.NewBigCache(config)
cache.Stats()
Output: {"hits":1222,"misses":2,"delete_hits":0,"delete_misses":0,"collisions":0}
but when i run the method Reset to flush the shards, they are not flushed / empty, , in previously versions this method works correctly.
cache, initErr := bigcache.NewBigCache(config)
cache.Reset()
After i run the Reset() the stats are se same
Output: {"hits":1222,"misses":2,"delete_hits":0,"delete_misses":0,"collisions":0}
Environment:
- Version (git sha or release): [email protected]
- OS (e.g. from
/etc/os-release
or winver.exe): Debian GNU/Linux 10 (buster) - go version: 1.18.3
Hi, I created following tests and it looks like stats were never cleared. What version do you mean in this sentence:
in previously versions this method works correctly
package bigcache
import (
"fmt"
"testing"
"time"
)
func TestCacheReset322(t *testing.T) {
t.Parallel()
// given
cache, _ := NewBigCache(Config{
Shards: 8,
LifeWindow: time.Second,
MaxEntriesInWindow: 1,
MaxEntrySize: 256,
})
keys := 1337
// when
for i := 0; i < keys; i++ {
cache.Set(fmt.Sprintf("key%d", i), []byte("value"))
cache.Get(fmt.Sprintf("key%d", i))
}
// then
if int64(keys) != cache.Stats().Hits {
t.Error("Stats not set")
}
// and when
cache.Reset()
// then
if int64(0) != cache.Stats().Hits {
t.Error("Stats not cleared")
}
}
Hello, I think this problem is because Reset() did not reset the cacheShard.Stats
func (c *BigCache) Reset() error {
for _, shard := range c.shards {
shard.reset(c.config)
}
return nil
}
func (s *cacheShard) reset(config Config) {
s.lock.Lock()
s.hashmap = make(map[uint64]uint32, config.initialShardSize())
s.entryBuffer = make([]byte, config.MaxEntrySize+headersSizeInBytes)
s.entries.Reset()
s.lock.Unlock()
}