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

The OnEvicted function is not called if a value is re-set after expiration but before deletion #48

Open
vic4hub opened this issue Feb 28, 2017 · 9 comments · May be fixed by #132
Open
Labels
Milestone

Comments

@vic4hub
Copy link

vic4hub commented Feb 28, 2017

Hello, in my testing, my simple function which prints integer counts for a key to be evicted (set via .OnEvicted) is not happening some of the time, despite eviction happening as expected. I am using GO 1.7

@patrickmn
Copy link
Owner

Please provide an example of failing code.

@vic4hub
Copy link
Author

vic4hub commented Feb 28, 2017

package main

import (
	"time"
	"log"
	"github.com/patrickmn/go-cache"
)

func main() {

	gc := cache.New(5 * time.Second, 3 * time.Second)
	gc.OnEvicted(func(k string, v interface{}) {
		log.Println("evicting key: ", k, " value: ", v.(int))
	})
	Write(gc)
	time.Sleep(1 * time.Hour)
}

func Write(gc *cache.Cache) {
	for i := 1; i <= 40; i++ {
		cnt , itemErr := gc.IncrementInt("key1", 1);
		if  itemErr != nil{
			println(itemErr.Error())
			gc.Set("key1", 1, 5 * time.Second)
		}else{
			log.Println(cnt)
		}
		time.Sleep(300 * time.Millisecond)
	}
}

@patrickmn
Copy link
Owner

What exactly are you expecting to see, and what happens instead?

@vic4hub
Copy link
Author

vic4hub commented Feb 28, 2017

When provided, it is expected for eviction function to run every time key:val pair is evicted - however, if you run my code, you will see it does not happen.

@patrickmn
Copy link
Owner

This is because eviction happens during the cleanup, but the value is re-set after the item has expired and before the cleanup has run.

I agree this is a bug.

@patrickmn patrickmn changed the title OnEvicted function is not called consistently The OnEvicted function is not called if a value is re-set after expiration but before deletion Feb 28, 2017
@vic4hub
Copy link
Author

vic4hub commented Feb 28, 2017

I see where it happens, got to change increment/decrement logic from "if !found || v.Expired() {" to just "if !found" throughout

@patrickmn
Copy link
Owner

You don't want to return values that have expired, though. Probably the fix is to check if a value exists but has expired in the Set functions, rather than just overwriting immediately (resulting in the evictor not seeing the old value and therefore not calling the OnExpired function on it.)

@patrickmn patrickmn added the bug label Apr 18, 2017
@patrickmn patrickmn added this to the 2.1.0 milestone Apr 19, 2017
@jerrypeng
Copy link

@patrickmn any updates on getting this fixed?

jaimem88 added a commit to jaimem88/go-cache that referenced this issue Oct 13, 2020
Check if an item exists and it has expired before setting so that
onEvicted is actually called.

Fixes patrickmn#48.
@jaimem88 jaimem88 linked a pull request Oct 13, 2020 that will close this issue
jaimem88 added a commit to jaimem88/go-cache that referenced this issue Oct 13, 2020
Check if an item exists and it has expired before setting so that
onEvicted is actually called.

Fixes patrickmn#48.

Add test and split func

Use nanoseconds instead
@ayufan
Copy link

ayufan commented Oct 14, 2020

We made that fix #132 for this issue :)

jaimem88 added a commit to jaimem88/go-cache that referenced this issue Oct 14, 2020
Check if an item exists and it has expired before setting so that
onEvicted is actually called.

Fixes patrickmn#48.

Add test and split func

Use nanoseconds instead

Call onEvicted if item existed before setting

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

Successfully merging a pull request may close this issue.

4 participants