allegro / allegro/bigcache

feature request: GetOrSet (w/ sync.Map.LoadOrStore semantics)

未關閉
#215 7 則留言 4 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Go
星號
8.2k
分支
614
平均合併
5 天 12 小時
30 天內合併 PR
1

描述

Use case:
(1) goroutine1 checks for key K in the cache; it doesn't exist, dispatches a request w/ "cache-miss" flag
(2) goroutine2 (dispatcher) sees "cache-miss", computes needed value, saves it in cache

Using Get for (1) and Set for (2) there's a potential race wherein (1) could check for the same key K multiple times before (2) runs. This is more interesting when computing needed values result in resource accounting/allocation from a third party component (read: allocated "value" must be released when they are no longer needed, otherwise there's a resource leak). The behavior of Set assumes that it's safe to overwrite existing values, with no notification to the caller that a pre-existing value may have been discarded.

To safeguard against this today, (2) could issue a Get before a Set to check for the described race, but this results in additional locking overhead vs. if Bigcache provided a GetOrSet call in the spirit of sync.Map's LoadOrStore. Or, alternatively, perhaps a SetWithInfo call that includes the value that was overwritten. It's arguable that the locking overhead of Get-before-Set may be negligible if the cost to compute the value is high enough. Perhaps this is a case of YMMV.

There are probably other use cases, this happens to be the one that I'm dealing with today.

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。