-
Notifications
You must be signed in to change notification settings - Fork 874
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
Comments
Please provide an example of failing code. |
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)
}
} |
What exactly are you expecting to see, and what happens instead? |
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. |
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. |
I see where it happens, got to change increment/decrement logic from "if !found || v.Expired() {" to just "if !found" throughout |
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 any updates on getting this fixed? |
Check if an item exists and it has expired before setting so that onEvicted is actually called. Fixes patrickmn#48.
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
We made that fix #132 for this issue :) |
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
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
The text was updated successfully, but these errors were encountered: