Theine v0.2.6 发布,新增缓存持久化功能

2023-05-19 10:27:36 +08:00
 matrix1010

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

持久化 API 的写入读取都需要 2 个参数。第一个参数是 version ,SaveCache 时会写入 Writer ,LoadCache 时会读取持久化的 version 然后与传入的 version 对比。 第二个参数是 writer/reader 。Theine 选择 io.Writer/io.Reader interface 来提供最大灵活性。一些现有的 cache 比如 fastcache 也提供持久化功能,但只接受文件路径。

API:

func (c *Cache[K, V]) SaveCache(version uint64, writer io.Writer) error
func (c *Cache[K, V]) LoadCache(version uint64, reader io.Reader) error

特性:

另外还有一个小优化,loading cache 使用 singleflight 来避免 thundering herd 。原先的 singleflight 是我直接复制源码套了层 generic ,但发现在大量写入情况下 allocation 较高,因为每次 singleflight Do 的时候都会新建一个 call 。于是加了一个 call 的 sync pool 。如果你在使用 Theine 的 loading cache 功能建议升级到 v0.2.6 。

1035 次点击
所在节点    Go 编程语言
0 条回复

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

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

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

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

© 2021 V2EX