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

Eviction not perform after timeout #31

Closed
gangbanlau opened this issue Feb 16, 2017 · 6 comments
Closed

Eviction not perform after timeout #31

gangbanlau opened this issue Feb 16, 2017 · 6 comments

Comments

@gangbanlau
Copy link

gangbanlau commented Feb 16, 2017

I am new to golang and looking for a cache library.
after read your blog, I write a small code for study cache set/get/expire. Now I have a issue, expire not working after timeout. still can get entry from bigcache. Did I missing something?

package main

import (
	"fmt"
	"github.com/allegro/bigcache"
	"log"
	"time"
)

func main() {
	lifetime := 10 * time.Second

/*
	onRemove := func(key string, entry []byte) {
		log.Println(key)
	}
	config := bigcache.Config{
		// number of shards (must be a power of 2)
		Shards: 1024,
		// time after which entry can be evicted
		LifeWindow: lifetime,
		// rps * lifeWindow, used only in initial memory allocation
		MaxEntriesInWindow: 1000 * 10 * 60,
		// max entry size in bytes, used only in initial memory allocation
		MaxEntrySize: 500,
		// prints information about additional memory allocation
		Verbose: true,
		// cache will not allocate more memory than this limit, value in MB
		// if value is reached then the oldest entries can be overridden for the new ones
		// 0 value means no size limit
		HardMaxCacheSize: 8192,
		// callback fired when the oldest entry is removed because of its
		// expiration time or no space left for the new entry. Default value is nil which
		// means no callback and it prevents from unwrapping the oldest entry.
		OnRemove: onRemove,
	}

	cache, initErr := bigcache.NewBigCache(config)
*/
    cache, initErr := bigcache.NewBigCache(bigcache.DefaultConfig(10 * time.Second))
	if initErr != nil {
		log.Fatal(initErr)
	}

	cache.Set("my-unique-key", []byte("value"))

	if entry, err := cache.Get("my-unique-key"); err == nil {
		fmt.Println(string(entry))
	}

	timer := time.NewTimer(lifetime + time.Second)
	<-timer.C

	// Eviction is performed during writes to the cache since the lock is already acquired.
	cache.Set("key2", []byte("val2"))

	if entry2, err2 := cache.Get("my-unique-key"); err2 == nil {
		fmt.Println(string(entry2))
	} else {
		log.Println("not found")
	}
}
@janisz
Copy link
Collaborator

janisz commented Feb 16, 2017

You got us :) We are really lazy and eviction is performed only when new item is inserted. If you try to obtain value from cache and it should be evicted but nothing was inserted you will get your value.
Maybe we should improve documentation of LifeWindow to make it clear that eviction could never happen.

@gangbanlau
Copy link
Author

Yes ,I known your eviction design after reading your blog. So my testing code already call a cache set action after timeout. And then try to query timeouted entry.

// Eviction is performed during writes to the cache since the lock is already acquired.
cache.Set("key2", []byte("val2"))

@janisz
Copy link
Collaborator

janisz commented Feb 17, 2017

You are correct. The problem is we only delete oldest entry from shard where we are going to insert new value. In your example you have 1024 shards and values get into different shards.

I've prepared test for this case: #33

@flisky
Copy link
Contributor

flisky commented Aug 4, 2017

I'm trying to support expiration in #41, and PTAL.

@navin89
Copy link

navin89 commented Feb 19, 2019

You got us :) We are really lazy and eviction is performed only when new item is inserted. If you try to obtain value from cache and it should be evicted but nothing was inserted you will get your value.
Maybe we should improve documentation of LifeWindow to make it clear that eviction could never happen.

Yes that would help others.

@rish8089dunzo
Copy link

rish8089dunzo commented Aug 23, 2020

You got us :) We are really lazy and eviction is performed only when new item is inserted. If you try to obtain value from cache and it should be evicted but nothing was inserted you will get your value.
Maybe we should improve documentation of LifeWindow to make it clear that eviction could never happen.

@janisz , In my project I need to ensure that keys are evicted after fixed time interval. I got your point that we need to set a new key to evict, but where should I write the code to add the new entry so that it evicts old entries.

Does this set operation evict only one old entry?

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

No branches or pull requests

5 participants