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>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
e0e0b57b8b
Коммит
9447cb9074
@@ -63,14 +63,9 @@ func (ps *PlatformService) ClearUserSessionCacheLocal(userID string) {
|
||||
return nil
|
||||
}
|
||||
|
||||
toPass := make([]any, 0, len(keys))
|
||||
for i := 0; i < len(keys); i++ {
|
||||
// This always needs to be a pointer to a value.
|
||||
// Otherwise the msp unmarshaler will fail to work.
|
||||
var session model.Session
|
||||
toPass = append(toPass, &session)
|
||||
}
|
||||
|
||||
// This always needs to be model.Session, not *model.Session.
|
||||
// Otherwise the msp unmarshaler will fail to work.
|
||||
toPass := allocateCacheTargets[model.Session](len(keys))
|
||||
errs := ps.sessionCache.GetMulti(keys, toPass)
|
||||
for i, err := range errs {
|
||||
if err != nil {
|
||||
|
||||
@@ -46,11 +46,7 @@ func (ps *PlatformService) GetAllStatuses() map[string]*model.Status {
|
||||
return nil
|
||||
}
|
||||
|
||||
toPass := make([]any, 0, len(keys))
|
||||
for i := 0; i < len(keys); i++ {
|
||||
var status *model.Status
|
||||
toPass = append(toPass, &status)
|
||||
}
|
||||
toPass := allocateCacheTargets[*model.Status](len(keys))
|
||||
errs := ps.statusCache.GetMulti(keys, toPass)
|
||||
for i, err := range errs {
|
||||
if err != nil {
|
||||
@@ -84,11 +80,7 @@ func (ps *PlatformService) GetStatusesByIds(userIDs []string) (map[string]any, *
|
||||
metrics := ps.Metrics()
|
||||
missingUserIds := []string{}
|
||||
|
||||
toPass := make([]any, 0, len(userIDs))
|
||||
for i := 0; i < len(userIDs); i++ {
|
||||
var status *model.Status
|
||||
toPass = append(toPass, &status)
|
||||
}
|
||||
toPass := allocateCacheTargets[*model.Status](len(userIDs))
|
||||
// First, we do a GetMulti to get all the status objects.
|
||||
errs := ps.statusCache.GetMulti(userIDs, toPass)
|
||||
for i, err := range errs {
|
||||
@@ -147,11 +139,7 @@ func (ps *PlatformService) GetUserStatusesByIds(userIDs []string) ([]*model.Stat
|
||||
metrics := ps.Metrics()
|
||||
|
||||
missingUserIds := []string{}
|
||||
toPass := make([]any, 0, len(userIDs))
|
||||
for i := 0; i < len(userIDs); i++ {
|
||||
var status *model.Status
|
||||
toPass = append(toPass, &status)
|
||||
}
|
||||
toPass := allocateCacheTargets[*model.Status](len(userIDs))
|
||||
// First, we do a GetMulti to get all the status objects.
|
||||
errs := ps.statusCache.GetMulti(userIDs, toPass)
|
||||
for i, err := range errs {
|
||||
|
||||
@@ -20,3 +20,13 @@ func maxInt(a, b int) int {
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Ссылка в новой задаче
Block a user