feature request: GetOrSet (w/ sync.Map.LoadOrStore semantics)
- Vorherrschende Sprache
- Go
- Sterne
- 8.2k
- Forks
- 614
- Ø Merge
- 5 T. 12 Std.
- Gemergte PRs (30 T.)
- 1
Beschreibung
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.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Bewertung
Dieses Issue wurde noch nicht bewertet.