Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: cache subject empty meta tags list #669

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/cachekey/cachekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ func User(id model.UserID) string {
}

func SubjectMetaTag(id model.SubjectID) string {
return resPrefix + "subject:meta-tags:" + strconv.FormatUint(uint64(id), 10)
return "chii:v0:subject:meta-tags:" + strconv.FormatUint(uint64(id), 10)
}
1 change: 1 addition & 0 deletions internal/tag/cache_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (r cacheRepo) Get(ctx context.Context, id model.SubjectID) ([]Tag, error) {
return tags, nil
}

// GetByIDs also need to change version in [cachekey.SubjectMetaTag] if schema is changed.
func (r cacheRepo) GetByIDs(ctx context.Context, ids []model.SubjectID) (map[model.SubjectID][]Tag, error) {
var tags []cachedTags
result := make(map[model.SubjectID][]Tag, len(ids))
Expand Down
8 changes: 8 additions & 0 deletions internal/tag/mysql_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,13 @@ func (r mysqlRepo) GetByIDs(ctx context.Context, ids []model.SubjectID) (map[mod
})
}

// set empty slice for subjects without tags
// this help we cache them.
for _, id := range ids {
if _, ok := tags[id]; !ok {
tags[id] = []Tag{}
}
}

return tags, nil
}