V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
matrix1010
V2EX  ›  Go 编程语言

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

  •  1
     
  •   matrix1010 ·
    Yiling-J · 336 天前 · 969 次点击
    这是一个创建于 336 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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
    

    特性:

    • 在 SaveCache 过程中 Theine 会同时写入 data 的 checksum ,在 LoadCache 时也会先检查 checksum 是否符合。
    • 持久化使用 gob 进行序列化 /反序列化。所以使用这个功能的前提条件是你的 key/value 类型能够用 gob 处理。
    • 由于 Theine 是自适应 LFU/LRU 缓存,持久化并不是简单的 gob 一个 map ,而是同时保存 LFU/LRU 及自适应参数相关信息,同时 LoadCache 时会还原这些信息。

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

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2876 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:27 · PVG 22:27 · LAX 07:27 · JFK 10:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.