Skip to content

Commit

Permalink
refactor: use early return
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Nov 25, 2024
1 parent b532253 commit 713a905
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions internal/tag/cache_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ func (r cacheRepo) Get(ctx context.Context, id model.SubjectID) ([]Tag, error) {
}

func (r cacheRepo) GetByIDs(ctx context.Context, ids []model.SubjectID) (map[model.SubjectID][]Tag, error) {
TotalCount.Add(float64(len(ids)))

var tags []cachedTags
result := make(map[model.SubjectID][]Tag, len(ids))
if len(ids) == 0 {
return result, nil
}

TotalCount.Add(float64(len(ids)))

var tags []cachedTags

err := r.cache.MGet(ctx, lo.Map(ids, func(item model.SubjectID, index int) string {
return cachekey.SubjectMetaTag(item)
}), &tags)
Expand All @@ -127,20 +128,22 @@ func (r cacheRepo) GetByIDs(ctx context.Context, ids []model.SubjectID) (map[mod
}
}

if len(missing) != 0 {
missingFromCache, err := r.repo.GetByIDs(ctx, missing)
if len(missing) == 0 {
return result, nil
}

missingFromCache, err := r.repo.GetByIDs(ctx, missing)
if err != nil {
return nil, err
}
for id, tag := range missingFromCache {
result[id] = tag
err = r.cache.Set(ctx, cachekey.SubjectMetaTag(id), cachedTags{
ID: id,
Tags: tag,
}, time.Hour)
if err != nil {
return nil, err
}
for id, tag := range missingFromCache {
result[id] = tag
err = r.cache.Set(ctx, cachekey.SubjectMetaTag(id), cachedTags{
ID: id,
Tags: tag,
}, time.Hour)
if err != nil {
return nil, errgo.Wrap(err, "cache.Set")
}
return nil, errgo.Wrap(err, "cache.Set")
}
}

Expand Down

0 comments on commit 713a905

Please sign in to comment.