Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset() don't work #322

Open
abolinhas opened this issue Jun 24, 2022 · 2 comments
Open

Reset() don't work #322

abolinhas opened this issue Jun 24, 2022 · 2 comments
Labels

Comments

@abolinhas
Copy link

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
@abolinhas abolinhas added the bug label Jun 24, 2022
@janisz
Copy link
Collaborator

janisz commented Jul 21, 2022

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")
	}
}

@Hojun-Cho
Copy link
Contributor

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()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants