MCPcopy Create free account
hub / github.com/bufbuild/buf / GetOrAdd

Method GetOrAdd

private/pkg/cache/cache.go:34–49  ·  view source on GitHub ↗

GetOrAdd gets the value for the key, or calls getUncached to get a new value, and then caches the value. If getUncached calls another Cache, the order of GetOrAdd calls between caches must be preserved for lock ordering.

(key K, getUncached func() (V, error))

Source from the content-addressed store, hash-verified

32// If getUncached calls another Cache, the order of GetOrAdd calls between caches
33// must be preserved for lock ordering.
34func (c *Cache[K, V]) GetOrAdd(key K, getUncached func() (V, error)) (V, error) {
35 c.lock.RLock()
36 var result *result[V]
37 var ok bool
38 if c.store != nil {
39 result, ok = c.store[key]
40 }
41 c.lock.RUnlock()
42 if ok {
43 return result.value, result.err
44 }
45 c.lock.Lock()
46 value, err := c.getOrAddInsideWriteLock(key, getUncached)
47 c.lock.Unlock()
48 return value, err
49}
50
51func (c *Cache[K, V]) getOrAddInsideWriteLock(key K, getUncached func() (V, error)) (V, error) {
52 if c.store == nil {

Callers 5

getFileInfoMethod · 0.80
getModuleForFilePathMethod · 0.80

Calls 5

RUnlockMethod · 0.80
RLockMethod · 0.65
LockMethod · 0.65
UnlockMethod · 0.65

Tested by

no test coverage detected