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.42k stars 192 forks source link

[Suggestion] Ristretto Set may fail and it's the expected behavior #206

Open Yiling-J opened 1 year ago

Yiling-J commented 1 year ago

From Ristretto's Readme FAQ:

As for "shortcuts," the only thing Ristretto does that could be construed as one is dropping some Set calls

So both set and setTags may return false when using Ristretto. Maybe better to mention it in readme or do something in code.

And here is a simple parallel benchmark to demonstrate that:

func BenchmarkRistrettoSetParallel(b *testing.B) {
    ctx := context.Background()

    client, err := ristretto.NewCache(&ristretto.Config{
        NumCounters: 1000,
        MaxCost:     100,
        BufferItems: 64,
    })
    if err != nil {
        panic(err)
    }
    store := NewRistretto(client, nil)

    b.RunParallel(func(pb *testing.PB) {
        i := 0
        for pb.Next() {
            key := fmt.Sprintf("test-%d", i)
            value := []byte(fmt.Sprintf("value-%d", i))
            err := store.Set(ctx, key, value, lib_store.WithTags([]string{fmt.Sprintf("tag-%d", i)}))
            if err != nil {
                fmt.Print("x")
            }
            i++
        }
    })
}

you'll see some "x" when running it