github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/local_cache_supplier_roles.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package store 5 6 import ( 7 "context" 8 9 "github.com/mattermost/mattermost-server/model" 10 ) 11 12 func (s *LocalCacheSupplier) handleClusterInvalidateRole(msg *model.ClusterMessage) { 13 if msg.Data == CLEAR_CACHE_MESSAGE_DATA { 14 s.roleCache.Purge() 15 } else { 16 s.roleCache.Remove(msg.Data) 17 } 18 } 19 20 func (s *LocalCacheSupplier) RoleSave(ctx context.Context, role *model.Role, hints ...LayeredStoreHint) *LayeredStoreSupplierResult { 21 if len(role.Name) != 0 { 22 defer s.doInvalidateCacheCluster(s.roleCache, role.Name) 23 } 24 return s.Next().RoleSave(ctx, role, hints...) 25 } 26 27 func (s *LocalCacheSupplier) RoleGet(ctx context.Context, roleId string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult { 28 // Roles are cached by name, as that is most commonly how they are looked up. 29 // This means that no caching is supported on roles being looked up by ID. 30 return s.Next().RoleGet(ctx, roleId, hints...) 31 } 32 33 func (s *LocalCacheSupplier) RoleGetAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult { 34 // Roles are cached by name, as that is most commonly how they are looked up. 35 // This means that no caching is supported on roles being listed. 36 return s.Next().RoleGetAll(ctx, hints...) 37 } 38 39 func (s *LocalCacheSupplier) RoleGetByName(ctx context.Context, name string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult { 40 if result := s.doStandardReadCache(ctx, s.roleCache, name, hints...); result != nil { 41 return result 42 } 43 44 result := s.Next().RoleGetByName(ctx, name, hints...) 45 46 s.doStandardAddToCache(ctx, s.roleCache, name, result, hints...) 47 48 return result 49 } 50 51 func (s *LocalCacheSupplier) RoleGetByNames(ctx context.Context, roleNames []string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult { 52 var foundRoles []*model.Role 53 var rolesToQuery []string 54 55 for _, roleName := range roleNames { 56 if result := s.doStandardReadCache(ctx, s.roleCache, roleName, hints...); result != nil { 57 foundRoles = append(foundRoles, result.Data.(*model.Role)) 58 } else { 59 rolesToQuery = append(rolesToQuery, roleName) 60 } 61 } 62 63 result := s.Next().RoleGetByNames(ctx, rolesToQuery, hints...) 64 65 if result.Data != nil { 66 rolesFound := result.Data.([]*model.Role) 67 for _, role := range rolesFound { 68 res := NewSupplierResult() 69 res.Data = role 70 s.doStandardAddToCache(ctx, s.roleCache, role.Name, res, hints...) 71 } 72 result.Data = append(foundRoles, result.Data.([]*model.Role)...) 73 } 74 75 return result 76 } 77 78 func (s *LocalCacheSupplier) RoleDelete(ctx context.Context, roleId string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult { 79 result := s.Next().RoleDelete(ctx, roleId, hints...) 80 81 if result.Err == nil { 82 role := result.Data.(*model.Role) 83 s.doInvalidateCacheCluster(s.roleCache, role.Name) 84 } 85 86 return result 87 } 88 89 func (s *LocalCacheSupplier) RolePermanentDeleteAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult { 90 defer s.roleCache.Purge() 91 defer s.doClearCacheCluster(s.roleCache) 92 93 return s.Next().RolePermanentDeleteAll(ctx, hints...) 94 }