github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/store/localcachelayer/team_layer.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package localcachelayer
     5  
     6  import (
     7  	"github.com/mattermost/mattermost-server/v5/model"
     8  	"github.com/mattermost/mattermost-server/v5/store"
     9  )
    10  
    11  type LocalCacheTeamStore struct {
    12  	store.TeamStore
    13  	rootStore *LocalCacheStore
    14  }
    15  
    16  func (s *LocalCacheTeamStore) handleClusterInvalidateTeam(msg *model.ClusterMessage) {
    17  	if msg.Data == ClearCacheMessageData {
    18  		s.rootStore.teamAllTeamIdsForUserCache.Purge()
    19  	} else {
    20  		s.rootStore.teamAllTeamIdsForUserCache.Remove(msg.Data)
    21  	}
    22  }
    23  
    24  func (s LocalCacheTeamStore) ClearCaches() {
    25  	s.rootStore.teamAllTeamIdsForUserCache.Purge()
    26  	if s.rootStore.metrics != nil {
    27  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("All Team Ids for User - Purge")
    28  	}
    29  }
    30  
    31  func (s LocalCacheTeamStore) InvalidateAllTeamIdsForUser(userId string) {
    32  	s.rootStore.doInvalidateCacheCluster(s.rootStore.teamAllTeamIdsForUserCache, userId)
    33  	if s.rootStore.metrics != nil {
    34  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("All Team Ids for User - Remove by UserId")
    35  	}
    36  }
    37  
    38  func (s LocalCacheTeamStore) GetUserTeamIds(userID string, allowFromCache bool) ([]string, error) {
    39  	if !allowFromCache {
    40  		return s.TeamStore.GetUserTeamIds(userID, allowFromCache)
    41  	}
    42  
    43  	var userTeamIds []string
    44  	if err := s.rootStore.doStandardReadCache(s.rootStore.teamAllTeamIdsForUserCache, userID, &userTeamIds); err == nil {
    45  		return userTeamIds, nil
    46  	}
    47  
    48  	userTeamIds, err := s.TeamStore.GetUserTeamIds(userID, allowFromCache)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	if len(userTeamIds) > 0 {
    54  		s.rootStore.doStandardAddToCache(s.rootStore.teamAllTeamIdsForUserCache, userID, userTeamIds)
    55  	}
    56  
    57  	return userTeamIds, nil
    58  }
    59  
    60  func (s LocalCacheTeamStore) Update(team *model.Team) (*model.Team, error) {
    61  	var oldTeam *model.Team
    62  	var err error
    63  	if team.DeleteAt != 0 {
    64  		oldTeam, err = s.TeamStore.Get(team.Id)
    65  		if err != nil {
    66  			return nil, err
    67  		}
    68  	}
    69  
    70  	tm, err := s.TeamStore.Update(team)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	defer s.rootStore.doClearCacheCluster(s.rootStore.rolePermissionsCache)
    75  
    76  	if oldTeam != nil && oldTeam.DeleteAt == 0 {
    77  		s.rootStore.doClearCacheCluster(s.rootStore.teamAllTeamIdsForUserCache)
    78  	}
    79  
    80  	return tm, err
    81  }