eko / gocache

☔️ A complete Go cache library that brings you multiple ways of managing your caches
https://vincent.composieux.fr/article/i-wrote-gocache-a-complete-and-extensible-go-cache-library/
MIT License
2.4k stars 193 forks source link

Ristretto store doesn't work as expected #207

Closed yolossn closed 1 year ago

yolossn commented 1 year ago

Simply setting a value and getting it fails when using ristretto store.

Steps for Reproduction

package main

import (
    "context"
    "fmt"

    "github.com/dgraph-io/ristretto"
    "github.com/eko/gocache/lib/v4/cache"
    ristretto_store "github.com/eko/gocache/store/ristretto/v4"
)

func main() {
    ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
        NumCounters: 1000,
        MaxCost:     100,
        BufferItems: 64,
    })
    if err != nil {
        panic(err)
    }
    ristrettoStore := ristretto_store.NewRistretto(ristrettoCache)
    ch := cache.New[string](ristrettoStore)
    err = ch.Set(context.Background(), "test", "value")
    if err != nil {
        panic(fmt.Sprintf("err in set:%+v\n", err))
    }
    value, err := ch.Get(context.Background(), "test")
    if err != nil {
        panic(fmt.Sprintf("err in get:%+v\n", err))
    }
    fmt.Println("The value is ", value)
}

Expected behavior: Expected the value to be printed

Actual behavior: Getting an error "value not found in store"

Platforms: Windows WSL

Extras: I think the error is happening because ristretto expects to wait after a key is set, but the ristretto store implementation doesn't do that. Refer this example where the Wait() function is used after Set() for the value to pass through buffers. https://github.com/dgraph-io/ristretto#Example

wlxwlxwlx commented 1 year ago

I have also encountered this problem. May I ask how to solve it?

febelery commented 1 year ago
func main() {
    ristrettoCache, err := ristretto.NewCache(&ristretto.Config{
        NumCounters: 1000,
        MaxCost:     100,
        BufferItems: 64,
    })
    if err != nil {
        panic(err)
    }
    ristrettoStore := ristretto_store.NewRistretto(ristrettoCache)
    ch := cache.New[string](ristrettoStore)
    err = ch.Set(context.Background(), "test", "value")
    if err != nil {
        panic(fmt.Sprintf("err in set:%+v\n", err))
    }
    ristrettoCache.Wait()
    value, err := ch.Get(context.Background(), "test")
    if err != nil {
        panic(fmt.Sprintf("err in get:%+v\n", err))
    }
    fmt.Println("The value is ", value)
}

ristrettoCache.Wait() is the key

eko commented 1 year ago

Closing this issue as there is no issue with the Gocache library.

Maybe we could add an option to the Gocache's store configuration to automatically call Wait after each Set()?

Please feel free to open a pull request if you want to or reopen this issue if you still have any issue!

Thank you