If you absolutely must make sure you ever create 2 instances of any value
in the map then I guess you have to lock.
Otherwise you can maybe use map.LoadOrStore?

fre 1 sep. 2017 kl 00:18 skrev bep <bjorn.erik.peder...@gmail.com>:

> sync.Map in Go 1.9 is a little low on examples/doc in the wild, so I
> thought I should ask here.
>
> The new type is promoted as a replacement for  RWMutex in mostly read use
> cases with stable keys. I assume that a typical in memory cache would fit
> that description.
>
> With RWMutex, if the cost of creating the cache item is high, I would
> maybe do something ala:
>
> mu.RLock()
> // Check cache
> mu.RUnclock()
>
> // Return if found
>
> // If Not found:
> mu.Lock()
> defer mu.Unlock()
> // Double check cache, return if found
> // Create item and put in cache
>
>
>
>
> I don't see how the above can be written with a sync.Map without adding a
> mutex.
>
> bep
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to