eko/gocache

Ristretto store doesn't work as expected

yolossn opened this issue · 4 comments

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

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

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

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