dgryski / go-tinylfu

TinyLFU cache admission policy
MIT License
252 stars 34 forks source link

[potential bug (?)] Sets on an existing key does not overwrite the value with small LFU sizes (and nothing to evict from slru) #9

Closed 2785 closed 1 year ago

2785 commented 1 year ago
func main() {
    c := tinylfu.New(100, 10000)

    c.Add("foo", "bar")

    val, _ := c.Get("foo")
    fmt.Println(val)

    c.Add("foo", "baz")

    val, _ = c.Get("foo")
    fmt.Println(val)
}

the above snippet prints bar and bar when doing .Add() on the same key. This seems to be because the code here

https://github.com/dgryski/go-tinylfu/blob/master/tinylfu.go#L85-L113

dgryski commented 1 year ago

I'll take a look. Thanks for the detailed report.

dominikh commented 1 year ago

@dgryski any ETA on looking at it?

dgryski commented 1 year ago

The cache didn't handle "Add" well if a key was already in the cache. We now detect this case and handle list movements as if it was "Get".