MCPcopy Index your code
hub / github.com/keybase/client / Load

Method Load

go/libkb/burst_cache.go:61–99  ·  view source on GitHub ↗

Load item key from the burst cache. On a cache miss, load with the given loader function. Return the object as an interface{}, so the caller needs to cast out of this burst cache.

(ctx context.Context, key BurstCacheKey, loader BurstCacheLoader)

Source from the content-addressed store, hash-verified

59// Load item key from the burst cache. On a cache miss, load with the given loader function.
60// Return the object as an interface{}, so the caller needs to cast out of this burst cache.
61func (b *BurstCache) Load(ctx context.Context, key BurstCacheKey, loader BurstCacheLoader) (ret interface{}, err error) {
62 ctx = WithLogTag(ctx, "BC")
63 defer b.G().CVTrace(ctx, VLog0, fmt.Sprintf("BurstCache(%s)#Load(%s)", b.cacheName, key.String()), &err)()
64
65 lock := b.locktab.AcquireOnName(ctx, b.G(), key.String())
66 defer lock.Release(ctx)
67
68 b.G().VDL.CLogf(ctx, VLog0, "| past single-flight lock")
69
70 found := false
71 if val, ok := b.lru.Get(key.String()); ok {
72 b.G().VDL.CLogf(ctx, VLog0, "| found in LRU cache")
73 if tmp, ok := val.(*burstCacheObj); ok {
74 age := b.G().GetClock().Now().Sub(tmp.cachedAt)
75 if age < b.cacheLife {
76 b.G().VDL.CLogf(ctx, VLog0, "| cached object was fresh (loaded %v ago)", age)
77 ret = tmp.obj
78 found = true
79 } else {
80 b.G().VDL.CLogf(ctx, VLog0, "| cached object expired %v ago", (age - b.cacheLife))
81 b.lru.Remove(key.String())
82 }
83 } else {
84 b.G().Log.CErrorf(ctx, "| object in LRU was of wrong type")
85 }
86 } else {
87 b.G().VDL.CLogf(ctx, VLog0, "| object cache miss")
88 }
89
90 if !found {
91 ret, err = loader()
92 if err == nil {
93 b.G().VDL.CLogf(ctx, VLog0, "| caching object after successful fetch")
94 b.lru.Add(key.String(), &burstCacheObj{ret, b.G().GetClock().Now()})
95 }
96 }
97
98 return ret, err
99}

Callers

nothing calls this directly

Calls 13

WithLogTagFunction · 0.85
CVTraceMethod · 0.80
AcquireOnNameMethod · 0.80
GMethod · 0.65
StringMethod · 0.65
CLogfMethod · 0.65
GetMethod · 0.65
NowMethod · 0.65
GetClockMethod · 0.65
RemoveMethod · 0.65
CErrorfMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected