github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/store/localcachelayer/channel_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 LocalCacheChannelStore struct {
    12  	store.ChannelStore
    13  	rootStore *LocalCacheStore
    14  }
    15  
    16  func (s *LocalCacheChannelStore) handleClusterInvalidateChannelMemberCounts(msg *model.ClusterMessage) {
    17  	if msg.Data == CLEAR_CACHE_MESSAGE_DATA {
    18  		s.rootStore.channelMemberCountsCache.Purge()
    19  	} else {
    20  		s.rootStore.channelMemberCountsCache.Remove(msg.Data)
    21  	}
    22  }
    23  
    24  func (s *LocalCacheChannelStore) handleClusterInvalidateChannelPinnedPostCount(msg *model.ClusterMessage) {
    25  	if msg.Data == CLEAR_CACHE_MESSAGE_DATA {
    26  		s.rootStore.channelPinnedPostCountsCache.Purge()
    27  	} else {
    28  		s.rootStore.channelPinnedPostCountsCache.Remove(msg.Data)
    29  	}
    30  }
    31  
    32  func (s *LocalCacheChannelStore) handleClusterInvalidateChannelGuestCounts(msg *model.ClusterMessage) {
    33  	if msg.Data == CLEAR_CACHE_MESSAGE_DATA {
    34  		s.rootStore.channelGuestCountCache.Purge()
    35  	} else {
    36  		s.rootStore.channelGuestCountCache.Remove(msg.Data)
    37  	}
    38  }
    39  
    40  func (s *LocalCacheChannelStore) handleClusterInvalidateChannelById(msg *model.ClusterMessage) {
    41  	if msg.Data == CLEAR_CACHE_MESSAGE_DATA {
    42  		s.rootStore.channelByIdCache.Purge()
    43  	} else {
    44  		s.rootStore.channelByIdCache.Remove(msg.Data)
    45  	}
    46  }
    47  
    48  func (s LocalCacheChannelStore) ClearCaches() {
    49  	s.rootStore.doClearCacheCluster(s.rootStore.channelMemberCountsCache)
    50  	s.rootStore.doClearCacheCluster(s.rootStore.channelPinnedPostCountsCache)
    51  	s.rootStore.doClearCacheCluster(s.rootStore.channelGuestCountCache)
    52  	s.rootStore.doClearCacheCluster(s.rootStore.channelByIdCache)
    53  	s.ChannelStore.ClearCaches()
    54  	if s.rootStore.metrics != nil {
    55  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Channel Pinned Post Counts - Purge")
    56  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Channel Member Counts - Purge")
    57  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Channel Guest Count - Purge")
    58  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Channel - Purge")
    59  	}
    60  }
    61  
    62  func (s LocalCacheChannelStore) InvalidatePinnedPostCount(channelId string) {
    63  	s.rootStore.doInvalidateCacheCluster(s.rootStore.channelPinnedPostCountsCache, channelId)
    64  	if s.rootStore.metrics != nil {
    65  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Channel Pinned Post Counts - Remove by ChannelId")
    66  	}
    67  }
    68  
    69  func (s LocalCacheChannelStore) InvalidateMemberCount(channelId string) {
    70  	s.rootStore.doInvalidateCacheCluster(s.rootStore.channelMemberCountsCache, channelId)
    71  	if s.rootStore.metrics != nil {
    72  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Channel Member Counts - Remove by ChannelId")
    73  	}
    74  }
    75  
    76  func (s LocalCacheChannelStore) InvalidateGuestCount(channelId string) {
    77  	s.rootStore.doInvalidateCacheCluster(s.rootStore.channelGuestCountCache, channelId)
    78  	if s.rootStore.metrics != nil {
    79  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Channel Guests Count - Remove by channelId")
    80  	}
    81  }
    82  
    83  func (s LocalCacheChannelStore) InvalidateChannel(channelId string) {
    84  	s.rootStore.doInvalidateCacheCluster(s.rootStore.channelByIdCache, channelId)
    85  	if s.rootStore.metrics != nil {
    86  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Channel - Remove by ChannelId")
    87  	}
    88  }
    89  
    90  func (s LocalCacheChannelStore) GetMemberCount(channelId string, allowFromCache bool) (int64, *model.AppError) {
    91  	if allowFromCache {
    92  		var count int64
    93  		if err := s.rootStore.doStandardReadCache(s.rootStore.channelMemberCountsCache, channelId, &count); err == nil {
    94  			return count, nil
    95  		}
    96  	}
    97  	count, err := s.ChannelStore.GetMemberCount(channelId, allowFromCache)
    98  
    99  	if allowFromCache && err == nil {
   100  		s.rootStore.doStandardAddToCache(s.rootStore.channelMemberCountsCache, channelId, count)
   101  	}
   102  
   103  	return count, err
   104  }
   105  
   106  func (s LocalCacheChannelStore) GetGuestCount(channelId string, allowFromCache bool) (int64, *model.AppError) {
   107  	if allowFromCache {
   108  		var count int64
   109  		if err := s.rootStore.doStandardReadCache(s.rootStore.channelGuestCountCache, channelId, &count); err == nil {
   110  			return count, nil
   111  		}
   112  	}
   113  	count, err := s.ChannelStore.GetGuestCount(channelId, allowFromCache)
   114  
   115  	if allowFromCache && err == nil {
   116  		s.rootStore.doStandardAddToCache(s.rootStore.channelGuestCountCache, channelId, count)
   117  	}
   118  
   119  	return count, err
   120  }
   121  
   122  func (s LocalCacheChannelStore) GetMemberCountFromCache(channelId string) int64 {
   123  	var count int64
   124  	if err := s.rootStore.doStandardReadCache(s.rootStore.channelMemberCountsCache, channelId, &count); err == nil {
   125  		return count
   126  	}
   127  
   128  	count, err := s.GetMemberCount(channelId, true)
   129  	if err != nil {
   130  		return 0
   131  	}
   132  
   133  	return count
   134  }
   135  
   136  func (s LocalCacheChannelStore) GetPinnedPostCount(channelId string, allowFromCache bool) (int64, *model.AppError) {
   137  	if allowFromCache {
   138  		var count int64
   139  		if err := s.rootStore.doStandardReadCache(s.rootStore.channelPinnedPostCountsCache, channelId, &count); err == nil {
   140  			return count, nil
   141  		}
   142  	}
   143  
   144  	count, err := s.ChannelStore.GetPinnedPostCount(channelId, allowFromCache)
   145  
   146  	if err != nil {
   147  		return 0, err
   148  	}
   149  
   150  	if allowFromCache {
   151  		s.rootStore.doStandardAddToCache(s.rootStore.channelPinnedPostCountsCache, channelId, count)
   152  	}
   153  
   154  	return count, nil
   155  }
   156  
   157  func (s LocalCacheChannelStore) Get(id string, allowFromCache bool) (*model.Channel, error) {
   158  
   159  	if allowFromCache {
   160  		var cacheItem *model.Channel
   161  		if err := s.rootStore.doStandardReadCache(s.rootStore.channelByIdCache, id, &cacheItem); err == nil {
   162  			ch := cacheItem.DeepCopy()
   163  			return ch, nil
   164  		}
   165  	}
   166  
   167  	ch, err := s.ChannelStore.Get(id, allowFromCache)
   168  
   169  	if allowFromCache && err == nil {
   170  		s.rootStore.doStandardAddToCache(s.rootStore.channelByIdCache, id, ch)
   171  	}
   172  
   173  	return ch, err
   174  }
   175  
   176  func (s LocalCacheChannelStore) SaveMember(member *model.ChannelMember) (*model.ChannelMember, *model.AppError) {
   177  	member, err := s.ChannelStore.SaveMember(member)
   178  	if err != nil {
   179  		return nil, err
   180  	}
   181  	s.InvalidateMemberCount(member.ChannelId)
   182  	return member, nil
   183  }
   184  
   185  func (s LocalCacheChannelStore) SaveMultipleMembers(members []*model.ChannelMember) ([]*model.ChannelMember, *model.AppError) {
   186  	members, err := s.ChannelStore.SaveMultipleMembers(members)
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  	for _, member := range members {
   191  		s.InvalidateMemberCount(member.ChannelId)
   192  	}
   193  	return members, nil
   194  }
   195  
   196  func (s LocalCacheChannelStore) UpdateMember(member *model.ChannelMember) (*model.ChannelMember, *model.AppError) {
   197  	member, err := s.ChannelStore.UpdateMember(member)
   198  	if err != nil {
   199  		return nil, err
   200  	}
   201  	s.InvalidateMemberCount(member.ChannelId)
   202  	return member, nil
   203  }
   204  
   205  func (s LocalCacheChannelStore) UpdateMultipleMembers(members []*model.ChannelMember) ([]*model.ChannelMember, *model.AppError) {
   206  	members, err := s.ChannelStore.UpdateMultipleMembers(members)
   207  	if err != nil {
   208  		return nil, err
   209  	}
   210  	for _, member := range members {
   211  		s.InvalidateMemberCount(member.ChannelId)
   212  	}
   213  	return members, nil
   214  }
   215  
   216  func (s LocalCacheChannelStore) RemoveMember(channelId, userId string) *model.AppError {
   217  	err := s.ChannelStore.RemoveMember(channelId, userId)
   218  	if err != nil {
   219  		return err
   220  	}
   221  	s.InvalidateMemberCount(channelId)
   222  	return nil
   223  }
   224  
   225  func (s LocalCacheChannelStore) RemoveMembers(channelId string, userIds []string) *model.AppError {
   226  	err := s.ChannelStore.RemoveMembers(channelId, userIds)
   227  	if err != nil {
   228  		return err
   229  	}
   230  	s.InvalidateMemberCount(channelId)
   231  	return nil
   232  }