Ristretto是Dgraph团队开发的高性能并发内存缓存库,专为Go语言设计。它具有高吞吐量、低延迟和内存高效的特点,非常适合需要快速内存缓存的场景。
特性
-
高命中率 - 独特的准入/淘汰策略组合
- 淘汰策略:SampledLFU - 与精确LRU相当,在搜索和数据库跟踪上表现更好
- 准入策略:TinyLFU - 内存开销小(每个计数器12位)
-
高吞吐量 - 使用多种技术管理争用
-
基于成本的淘汰 - 任何被认为有价值的大项目可以淘汰多个小项目
-
完全并发 - 可以使用任意数量的goroutine而不会显著降低吞吐量
-
指标 - 可选的性能指标
-
简单API - 只需配置好参数即可使用
基本使用
安装
确保使用Go 1.21或更高版本,然后运行:
arduino
go get github.com/dgraph-io/ristretto/v2
版本选择:
- v1.x.x 是大多数程序使用的第一个版本
- v2.x.x 是支持泛型的新版本,推荐新项目使用
初始化缓存
go
import (
"fmt"
"github.com/dgraph-io/ristretto"
)
func main() {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // 键跟踪数 (10M)
MaxCost: 1 << 30, // 最大缓存成本 (1GB)
BufferItems: 64, // 每个分片的缓冲区大小
})
}
高级功能
1. 带TTL的缓存
arduino
// 设置10秒过期的缓存
cache.SetWithTTL("tempKey", "tempValue", 1, 10*time.Second)
2. 成本感知缓存
go
type LargeStruct struct {
Data []byte
}
// 根据数据大小设置成本
largeData := &LargeStruct{Data: make([]byte, 1024*1024)} // 1MB
cache.Set("largeKey", largeData, int64(len(largeData.Data)))
3. 回调函数
go
cache, _ := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e6,
MaxCost: 100 << 20, // 100MB
BufferItems: 64,
OnEvict: func(item *ristretto.Item) {
fmt.Printf("Evicted key: %v\n", item.Key)
},
})
4. 性能指标
css
metrics := cache.Metrics()
fmt.Printf("Hit Ratio: %.2f%%\n", metrics.Ratio()*100)
fmt.Println("Hits:", metrics.Hits())
fmt.Println("Misses:", metrics.Misses())
最佳实践
- 合理配置NumCounters:通常设置为预期最大缓存项数的10倍
- 设置适当的MaxCost:根据可用内存设置
- 批量操作:减少锁竞争
- 预热缓存:启动时加载热点数据
- 监控指标:定期检查命中率调整配置
性能对比
Ristretto在并发读写下表现优异:
- 比
sync.Map快约5-10倍 - 比原生map+mutex快约3-5倍
- 内存效率比简单的map实现高30-50%
完整示例
go
package main
import (
"fmt"
"time"
"github.com/dgraph-io/ristretto"
)
func main() {
// 初始化缓存 (100MB内存限制)
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e6, // 键跟踪数
MaxCost: 100 << 20, // 100MB
BufferItems: 64,
})
if err != nil {
panic(err)
}
// 并发写入
for i := 0; i < 10; i++ {
go func(n int) {
key := fmt.Sprintf("key%d", n)
cache.Set(key, fmt.Sprintf("value%d", n), 1)
}(i)
}
// 读取示例
time.Sleep(100 * time.Millisecond)
if value, found := cache.Get("key5"); found {
fmt.Println("Got:", value)
}
// 带TTL的缓存
cache.SetWithTTL("temp", "will expire", 1, 2*time.Second)
time.Sleep(3 * time.Second)
if _, found := cache.Get("temp"); !found {
fmt.Println("Item correctly expired")
}
// 性能指标
metrics := cache.Metrics()
fmt.Printf("Final Hit Ratio: %.2f%%\n", metrics.Ratio()*100)
}
性能基准
Ristretto在各种场景下都表现出色:
搜索工作负载的命中率

数据库工作负载的命中率

混合工作负载的吞吐量
