dgraph-io / ristretto

A high performance memory-bound Go cache
https://dgraph.io/blog/post/introducing-ristretto-high-perf-go-cache/
Apache License 2.0
5.64k stars 374 forks source link

[QUESTION]: Is it possible to get experinece similar to redis MGET #315

Closed dakait closed 3 months ago

dakait commented 1 year ago

Question.

Hi, I'm use local cache using Ristretto to reduce the latency of data retrieval. I'm retriving around 60000 cache keys per second at peak traffic. I tried searching but not able to find a multi get solution for retriving data so I'm looping over the number of cache keys and doing Get() to retrieve data from local cache. Sharing some comparison calculation for latency:

Reducing cache keys per request can help in reducing this but due to goroutines throttling added in system I can only take this upto 500 cache keys per request.

Code reference:

type RedisPair struct {
    Key              string
    Value            interface{}
    found            bool
}

func (l *localCache) MGet(ctx context.Context, pairs ...*RedisPair) bool {
    if len(pairs) == 0 {
        return true
    }

    allFound := true

    for _, pair := range pairs {
        found := l.Get(ctx, pair.Key, pair.Value)
        pair.found = found
        allFound = allFound && found
    }

    return allFound
}

func (l *localCache) Get(ctx context.Context, key string, obj interface{}) bool {
    val, found := l.Cache.Get(key)
    if found {
        err := l.unmarshal(val, obj)
        if err != nil {
            return false
        }

        return found
    }

    return false
}

How can I optimise my code so I don't have to loop for Get() operations?

github-actions[bot] commented 3 months ago

This issue has been stale for 60 days and will be closed automatically in 7 days. Comment to keep it open.