You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To start using Ristretto, install Go 1.21 or above. Ristretto needs go modules. From your project,
run the following command
go get github.com/dgraph-io/ristretto/v2
This will retrieve the library.
Choosing a version
Following these rules:
v1.x.x is the first version used in most programs with Ristretto dependencies.
v2.x.x is the new version with support for generics, for which it has a slightly different
interface. This version is designed to solve compatibility problems of programs using the old
version of Ristretto. If you start writing a new program, it is recommended to use this version.
Usage
package main
import (
"fmt""github.com/dgraph-io/ristretto/v2"
)
funcmain() {
cache, err:=ristretto.NewCache(&ristretto.Config[string, string]{
NumCounters: 1e7, // number of keys to track frequency of (10M).MaxCost: 1<<30, // maximum cost of cache (1GB).BufferItems: 64, // number of keys per Get buffer.
})
iferr!=nil {
panic(err)
}
defercache.Close()
// set a value with a cost of 1cache.Set("key", "value", 1)
// wait for value to pass through bufferscache.Wait()
// get value from cachevalue, found:=cache.Get("key")
if!found {
panic("missing value")
}
fmt.Println(value)
// del value from cachecache.Del("key")
}
Dgraph - Horizontally scalable and distributed GraphQL
database with a graph backend
FAQ
How are you achieving this performance? What shortcuts are you taking?
We go into detail in the
Ristretto blog post, but in
short: our throughput performance can be attributed to a mix of batching and eventual consistency.
Our hit ratio performance is mostly due to an excellent
admission policy and SampledLFU eviction policy.
As for "shortcuts," the only thing Ristretto does that could be construed as one is dropping some
Set calls. That means a Set call for a new item (updates are guaranteed) isn't guaranteed to make it
into the cache. The new item could be dropped at two points: when passing through the Set buffer or
when passing through the admission policy. However, this doesn't affect hit ratios much at all as we
expect the most popular items to be Set multiple times and eventually make it in the cache.
Is Ristretto distributed?
No, it's just like any other Go library that you can import into your project and use in a single
process.