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

Ristretto store doesn't work as expected #207

Closed
yolossn opened this issue May 6, 2023 · 3 comments · Fixed by #231
Closed

Ristretto store doesn't work as expected #207

yolossn opened this issue May 6, 2023 · 3 comments · Fixed by #231

Comments

@yolossn
Copy link

yolossn commented May 6, 2023

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
Copy link

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

@febelery
Copy link

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
Copy link
Owner

eko commented Aug 7, 2023

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

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

Successfully merging a pull request may close this issue.

4 participants