Theine - 新一代高性能&高命中率泛型内存缓存

2023-04-14 10:04:43 +08:00
 matrix1010

先上链接和 benchmarks:

https://github.com/Yiling-J/theine-go

cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz

BenchmarkGetTheineParallel-12           32432190                36.39 ns/op            0 B/op          0 allocs/op
BenchmarkGetRistrettoParallel-12        63978058                18.86 ns/op           17 B/op          1 allocs/op
BenchmarkSetTheineParallel-12           20791834                84.49 ns/op            0 B/op          0 allocs/op
BenchmarkSetRistrettoParallel-12        23354626                65.53 ns/op          116 B/op          3 allocs/op
BenchmarkZipfTheineParallel-12          14771362                74.72 ns/op            1 B/op          0 allocs/op
BenchmarkZipfRistrettoParallel-12       21031435                61.82 ns/op          100 B/op          3 allocs/op

由于充分利用 sync pool 以及 atomic ,Theine 的 GC 压力很低,同时有着和 Ristretto 同一数量级的性能。

相比于 Ristretto ,Theine 最大的特点其实是极高的缓存命中率,benchmark 结果都是图片,可以直接去 README 看: hit-ratios。简单来说 Ristretto 是通过丢弃请求来达到高性能,代价就是高写入下大量 Set 请求丢弃导致缓存命中率大幅下降。而 Theine 则通过合理的架构避免了这个问题。

同时 Theine 也有 Python 版本: https://github.com/Yiling-J/theine. 所以你也许会觉得看到这个名字眼熟。Theine-Go 的 TinyLFU 部分其实是直接从 Python 版本转过来的,但在提高性能方面 Python 和 Go 版本的 Theine 都根据对应的语言特性做了优化

3110 次点击
所在节点    Go 编程语言
26 条回复
matrix1010
2023-04-19 16:25:38 +08:00
@Kould 这令我想到了这几天在 zhihu 上看到的帖子,游戏服务器为什么不用 Redis 。毕竟互联网天天都和 Redis 打交道,面试也是各种卷 Redis ,这么信仰 Redis 也挺正常
cuebyte
2023-04-22 05:46:33 +08:00
支持!
zhujq
2023-04-25 17:21:12 +08:00
支持支持
zzl22100048
2023-04-28 00:01:08 +08:00
如果能加上一个过期回调就好了,这样可以当延迟队列用
matrix1010
2023-04-28 08:46:08 +08:00
@zzl22100048 最新版本已经加上回调了。不过由于 timingwheel 的特性过期回调触发的时间不是精确的,只能保证最终会发生
matrix1010
2023-05-02 10:13:42 +08:00
顺带补充一点,Redis 的 client-side caching 其实就是 redis+local 两级 cache ,缓存失效时 client1->redis server->client2 发通知。有些 redis 的 client 没有这个功能,有些则使用简单 LRU, 这也是 Theine 的潜在使用场景。client-side caching 的设计可以看: https://redis.io/docs/manual/client-side-caching/#the-redis-implementation-of-client-side-caching

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/932421

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX