MM-60171: Using a generic function to allocate values (#28245)

We sprinkle a bit of generic magic to refactor a lot
of duplicate code.

To avoid exposing unnecessary code, I duplicated the function
twice. But let me know if you have strong opinions about this.

https://mattermost.atlassian.net/browse/MM-60171

```release-note
NONE
```


---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Agniva De Sarker
2024-09-25 10:00:14 +05:30
коммит произвёл GitHub
родитель e0e0b57b8b
Коммит 9447cb9074
7 изменённых файлов: 33 добавлений и 62 удалений

Просмотреть файл

@@ -245,12 +245,7 @@ func (s LocalCacheChannelStore) GetMany(ids []string, allowFromCache bool) (mode
var channelsToQuery []string
if allowFromCache {
var toPass []any
for i := 0; i < len(ids); i++ {
var channel *model.Channel
toPass = append(toPass, &channel)
}
toPass := allocateCacheTargets[*model.Channel](len(ids))
errs := s.rootStore.doMultiReadCache(s.rootStore.roleCache, ids, toPass)
for i, err := range errs {
if err != nil {
@@ -352,12 +347,7 @@ func (s LocalCacheChannelStore) getByNames(teamId string, names []string, allowF
newKeys = append(newKeys, teamId+name)
}
toPass := make([]any, 0, len(newKeys))
for i := 0; i < len(newKeys); i++ {
var channel *model.Channel
toPass = append(toPass, &channel)
}
toPass := allocateCacheTargets[*model.Channel](len(newKeys))
errs := s.rootStore.doMultiReadCache(s.rootStore.roleCache, newKeys, toPass)
for i, err := range errs {
if err != nil {
@@ -474,12 +464,7 @@ func (s LocalCacheChannelStore) GetChannelsMemberCount(channelIDs []string) (_ m
counts := make(map[string]int64)
remainingChannels := make([]string, 0)
toPass := make([]any, 0, len(channelIDs))
for i := 0; i < len(channelIDs); i++ {
var cacheItem int64
toPass = append(toPass, &cacheItem)
}
toPass := allocateCacheTargets[int64](len(channelIDs))
errs := s.rootStore.doMultiReadCache(s.rootStore.reaction.rootStore.channelMemberCountsCache, channelIDs, toPass)
for i, err := range errs {
if err != nil {

Просмотреть файл

@@ -579,3 +579,13 @@ func (s *LocalCacheStore) Invalidate() {
s.doClearCacheCluster(s.teamAllTeamIdsForUserCache)
s.doClearCacheCluster(s.rolePermissionsCache)
}
// allocateCacheTargets is used to fill target value types
// for getting items from cache.
func allocateCacheTargets[T any](l int) []any {
toPass := make([]any, 0, l)
for i := 0; i < l; i++ {
toPass = append(toPass, new(T))
}
return toPass
}

Просмотреть файл

@@ -62,11 +62,7 @@ func (s LocalCacheRoleStore) GetByNames(names []string) ([]*model.Role, error) {
var foundRoles []*model.Role
var rolesToQuery []string
toPass := make([]any, 0, len(names))
for i := 0; i < len(names); i++ {
var role *model.Role
toPass = append(toPass, &role)
}
toPass := allocateCacheTargets[*model.Role](len(names))
errs := s.rootStore.doMultiReadCache(s.rootStore.roleCache, names, toPass)
for i, err := range errs {
if err != nil {

Просмотреть файл

@@ -82,11 +82,7 @@ func (s *LocalCacheUserStore) InvalidateProfilesInChannelCacheByUser(userId stri
return nil
}
toPass := make([]any, 0, len(keys))
for i := 0; i < len(keys); i++ {
var userMap model.UserMap
toPass = append(toPass, &userMap)
}
toPass := allocateCacheTargets[model.UserMap](len(keys))
errs := s.rootStore.doMultiReadCache(s.rootStore.profilesInChannelCache, keys, toPass)
for i, err := range errs {
if err != nil {
@@ -177,11 +173,7 @@ func (s *LocalCacheUserStore) GetProfileByIds(ctx context.Context, userIds []str
remainingUserIds := make([]string, 0)
fromMaster := false
toPass := make([]any, 0, len(userIds))
for i := 0; i < len(userIds); i++ {
var user *model.User
toPass = append(toPass, &user)
}
toPass := allocateCacheTargets[*model.User](len(userIds))
errs := s.rootStore.doMultiReadCache(s.rootStore.userProfileByIdsCache, userIds, toPass)
for i, err := range errs {
if err != nil {
@@ -263,12 +255,7 @@ func (s *LocalCacheUserStore) GetMany(ctx context.Context, ids []string) ([]*mod
uniqIDs := dedup(ids)
fromMaster := false
toPass := make([]any, 0, len(uniqIDs))
for i := 0; i < len(uniqIDs); i++ {
var user *model.User
toPass = append(toPass, &user)
}
toPass := allocateCacheTargets[*model.User](len(uniqIDs))
errs := s.rootStore.doMultiReadCache(s.rootStore.userProfileByIdsCache, uniqIDs, toPass)
for i, err := range errs {
if err != nil {