dgraph-io/ristretto
GitHub: dgraph-io/ristretto
Stars: 6916 | Forks: 438
# Ristretto
[](https://github.com/dgraph-io/ristretto?tab=Apache-2.0-1-ov-file#readme)
[](https://github.com/dgraph-io/ristretto/stargazers)
[](https://github.com/dgraph-io/ristretto/commits/main/)
[](https://goreportcard.com/report/github.com/dgraph-io/ristretto)
Ristretto is a fast, concurrent cache library built with a focus on performance and correctness.
The motivation to build Ristretto comes from the need for a contention-free cache in [Dgraph][].
## Features
- **High Hit Ratios** - with our unique admission/eviction policy pairing, Ristretto's performance
is best in class.
- **Eviction: SampledLFU** - on par with exact LRU and better performance on Search and Database
traces.
- **Admission: TinyLFU** - extra performance with little memory overhead (12 bits per counter).
- **Fast Throughput** - we use a variety of techniques for managing contention and the result is
excellent throughput.
- **Cost-Based Eviction** - any large new item deemed valuable can evict multiple smaller items
(cost could be anything).
- **Fully Concurrent** - you can use as many goroutines as you want with little throughput
degradation.
- **Metrics** - optional performance metrics for throughput, hit ratios, and other stats.
- **Simple API** - just figure out your ideal `Config` values and you're off and running.
## Status
Ristretto is production-ready. See [Projects using Ristretto](#projects-using-ristretto).
## Getting Started
### Installing
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"
)
func main() {
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.
})
if err != nil {
panic(err)
}
defer cache.Close()
// set a value with a cost of 1
cache.Set("key", "value", 1)
// wait for value to pass through buffers
cache.Wait()
// get value from cache
value, found := cache.Get("key")
if !found {
panic("missing value")
}
fmt.Println(value)
// del value from cache
cache.Del("key")
}
## Benchmarks
The benchmarks can be found in
https://github.com/dgraph-io/dgraph-benchmarks/tree/main/cachebench/ristretto.
### Hit Ratios for Search
This trace is described as "disk read accesses initiated by a large commercial search engine in
response to various web search requests."
标签:EVTX分析