Skip to content

Commit

Permalink
better var name and cleanup
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent Composieux <vincent.composieux@gmail.com>
  • Loading branch information
advoretsky and eko authored Apr 16, 2024
1 parent 94a8f37 commit 9695206
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/cache/loadable.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,21 @@ func (c *LoadableCache[T]) Get(ctx context.Context, key any) (T, error) {
}

// Unable to find in cache, try to load it from load function
var r any
if r, err, _ = c.singleFlight.Do(
c.getCacheKey(key),
cacheKey := c.getCacheKey(key)
zero := *new(T)

loadedResult, err, _ := c.singleFlight.Do(
cacheKey,
func() (any, error) {
return c.loadFunc(ctx, key)
},
); err != nil {
return *new(T), err
)
if err != nil {
return zero, err
}

var ok bool
if object, ok = r.(T); !ok {
zero := *new(T)
if object, ok = loadedResult.(T); !ok {
return zero, errors.New(
fmt.Sprintf("returned value can't be cast to %T", zero),
)
Expand Down

0 comments on commit 9695206

Please sign in to comment.