allegro / allegro/bigcache

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

Aperta
#215 7 commenti 4 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Go
Stelle
8.2k
Fork
614
Merge medio
5g 12h
PR unite (30g)
1

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.