github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/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 == ClearCacheMessageData { 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 == ClearCacheMessageData { 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 == ClearCacheMessageData { 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 == ClearCacheMessageData { 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, error) { 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, error) { 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, error) { 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 return cacheItem, nil 163 } 164 } 165 166 ch, err := s.ChannelStore.Get(id, allowFromCache) 167 168 if allowFromCache && err == nil { 169 s.rootStore.doStandardAddToCache(s.rootStore.channelByIdCache, id, ch) 170 } 171 172 return ch, err 173 } 174 175 func (s LocalCacheChannelStore) SaveMember(member *model.ChannelMember) (*model.ChannelMember, error) { 176 member, err := s.ChannelStore.SaveMember(member) 177 if err != nil { 178 return nil, err 179 } 180 s.InvalidateMemberCount(member.ChannelId) 181 return member, nil 182 } 183 184 func (s LocalCacheChannelStore) SaveMultipleMembers(members []*model.ChannelMember) ([]*model.ChannelMember, error) { 185 members, err := s.ChannelStore.SaveMultipleMembers(members) 186 if err != nil { 187 return nil, err 188 } 189 for _, member := range members { 190 s.InvalidateMemberCount(member.ChannelId) 191 } 192 return members, nil 193 } 194 195 func (s LocalCacheChannelStore) UpdateMember(member *model.ChannelMember) (*model.ChannelMember, error) { 196 member, err := s.ChannelStore.UpdateMember(member) 197 if err != nil { 198 return nil, err 199 } 200 s.InvalidateMemberCount(member.ChannelId) 201 return member, nil 202 } 203 204 func (s LocalCacheChannelStore) UpdateMultipleMembers(members []*model.ChannelMember) ([]*model.ChannelMember, error) { 205 members, err := s.ChannelStore.UpdateMultipleMembers(members) 206 if err != nil { 207 return nil, err 208 } 209 for _, member := range members { 210 s.InvalidateMemberCount(member.ChannelId) 211 } 212 return members, nil 213 } 214 215 func (s LocalCacheChannelStore) RemoveMember(channelId, userId string) error { 216 err := s.ChannelStore.RemoveMember(channelId, userId) 217 if err != nil { 218 return err 219 } 220 s.InvalidateMemberCount(channelId) 221 return nil 222 } 223 224 func (s LocalCacheChannelStore) RemoveMembers(channelId string, userIds []string) error { 225 err := s.ChannelStore.RemoveMembers(channelId, userIds) 226 if err != nil { 227 return err 228 } 229 s.InvalidateMemberCount(channelId) 230 return nil 231 }