github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/store/localcachelayer/branch_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/vnforks/kid/v5/model" 8 "github.com/vnforks/kid/v5/store" 9 ) 10 11 type LocalCacheBranchStore struct { 12 store.BranchStore 13 rootStore *LocalCacheStore 14 } 15 16 func (s *LocalCacheBranchStore) handleClusterInvalidateBranch(msg *model.ClusterMessage) { 17 if msg.Data == CLEAR_CACHE_MESSAGE_DATA { 18 s.rootStore.branchAllBranchIdsForUserCache.Purge() 19 } else { 20 s.rootStore.branchAllBranchIdsForUserCache.Remove(msg.Data) 21 } 22 } 23 24 func (s LocalCacheBranchStore) ClearCaches() { 25 s.rootStore.branchAllBranchIdsForUserCache.Purge() 26 if s.rootStore.metrics != nil { 27 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("All Branch Ids for User - Purge") 28 } 29 } 30 31 func (s LocalCacheBranchStore) InvalidateAllBranchIdsForUser(userId string) { 32 s.rootStore.doInvalidateCacheCluster(s.rootStore.branchAllBranchIdsForUserCache, userId) 33 if s.rootStore.metrics != nil { 34 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("All Branch Ids for User - Remove by UserId") 35 } 36 } 37 38 func (s LocalCacheBranchStore) GetUserBranchIds(userID string, allowFromCache bool) ([]string, *model.AppError) { 39 if !allowFromCache { 40 return s.BranchStore.GetUserBranchIds(userID, allowFromCache) 41 } 42 43 if userBranchIds := s.rootStore.doStandardReadCache(s.rootStore.branchAllBranchIdsForUserCache, userID); userBranchIds != nil { 44 return userBranchIds.([]string), nil 45 } 46 47 userBranchIds, err := s.BranchStore.GetUserBranchIds(userID, allowFromCache) 48 if err != nil { 49 return nil, err 50 } 51 52 if len(userBranchIds) > 0 { 53 s.rootStore.doStandardAddToCache(s.rootStore.branchAllBranchIdsForUserCache, userID, userBranchIds) 54 } 55 56 return userBranchIds, nil 57 } 58 59 func (s LocalCacheBranchStore) Update(branch *model.Branch) (*model.Branch, *model.AppError) { 60 var oldBranch *model.Branch 61 var err *model.AppError 62 if branch.DeleteAt != 0 { 63 oldBranch, err = s.BranchStore.Get(branch.Id) 64 if err != nil { 65 return nil, err 66 } 67 } 68 69 tm, err := s.BranchStore.Update(branch) 70 if err != nil { 71 return nil, err 72 } 73 defer s.rootStore.doClearCacheCluster(s.rootStore.rolePermissionsCache) 74 75 if oldBranch != nil && oldBranch.DeleteAt == 0 { 76 s.rootStore.doClearCacheCluster(s.rootStore.branchAllBranchIdsForUserCache) 77 } 78 79 return tm, err 80 }