golang中的重复缓存

时间:2019-05-03 04:51:29

标签: go lru

尝试通过使用这两个库https://github.com/patrickmn/go-cachehttps://github.com/karlseguin/ccache之一的l1缓存解决简单的重复检查

执行了多个goroutine,它们从上游API提取大量数据。每个循环内部都有一个for range循环,用于检查按键是命中还是未命中。万一遇到问题,循环应跳过并继续进行下一次迭代。您可能已经猜到了,缓存应该是并发安全的。我还仅实例化了此缓存的一个实例(x.cacheInstance)。

for _, record := range data {
   ...
   exists := x.cacheInstance.Get(record.DocumentID)
        if exists == nil {
            x.cacheInstance.Set(record.DocumentID, true, time.Minute * 10)
        } else {
            c.logger.Warnw("DocumentID duplicate located", "documentID", record.DocumentID)
            continue
        }
   ...
}

但是,该应用程序似乎挂起了,并没有继续遍历记录。注释lru缓存检查可解决此问题,使程序可以按预期运行。

0 个答案:

没有答案