dgraph-io/ristretto
GitHub: dgraph-io/ristretto
Ristretto 是一款高性能、支持完全并发的 Go 内存缓存库,通过 TinyLFU 准入与 SampledLFU 驱逐策略实现行业领先的缓存命中率。
Stars: 6967 | Forks: 444
# 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 是一个快速、并发的缓存库,其构建重点是性能和正确性。
构建 Ristretto 的动机来自于 [Dgraph][] 中对无竞争缓存的需求。
## 功能
- **高命中率** - 得益于我们独特的准入/驱逐策略组合,Ristretto 的性能是同类中最好的。
- **驱逐:SampledLFU** - 与精确的 LRU 相当,并且在搜索和数据库跟踪记录上具有更好的性能。
- **准入:TinyLFU** - 以极小的内存开销(每个计数器 12 位)带来额外的性能提升。
- **高吞吐量** - 我们使用了多种技术来管理竞争,从而实现了出色的吞吐量。
- **基于成本的驱逐** - 任何被认为有价值的大型新项都可以驱逐多个较小的项(成本可以是任何东西)。
- **完全并发** - 你可以根据需要使用任意数量的 goroutine,而吞吐量几乎不会下降。
- **指标** - 用于吞吐量、命中率和其他统计数据的可选性能指标。
- **简单的 API** - 只需找出你理想的 `Config` 值,你就可以开始使用了。
## 状态
Ristretto 已达到生产就绪状态。请参阅[使用 Ristretto 的项目](#projects-using-ristretto)。
## 快速开始
### 安装
要开始使用 Ristretto,请安装 Go 1.21 或更高版本。Ristretto 需要 go modules。在你的项目中,运行以下命令:
```
go get github.com/dgraph-io/ristretto/v2
```
这将获取该库。
#### 选择版本
请遵循以下规则:
- v1.x.x 是大多数具有 Ristretto 依赖的程序中使用的第一个版本。
- v2.x.x 是支持泛型的新版本,因此它具有稍微不同的接口。此版本旨在解决使用旧版本 Ristretto 的程序的兼容性问题。如果你开始编写新程序,建议使用此版本。
## 用法
```
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")
}
```
## 基准测试
这些基准测试可以在
https://github.com/dgraph-io/dgraph-benchmarks/tree/main/cachebench/ristretto 中找到。
### 搜索的命中率
此跟踪记录被描述为“大型商业搜索引擎为响应各种网络搜索请求而发起的磁盘读取访问。”
标签:EVTX分析, Go, Ruby工具, 内存管理, 并发编程, 开发组件库, 日志审计, 缓存