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)
| 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. |
| 61 | func (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 | } |
nothing calls this directly
no test coverage detected