github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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.Id) != 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) RoleGetByName(ctx context.Context, name string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult { 34 if result := s.doStandardReadCache(ctx, s.roleCache, name, hints...); result != nil { 35 return result 36 } 37 38 result := s.Next().RoleGetByName(ctx, name, hints...) 39 40 s.doStandardAddToCache(ctx, s.roleCache, name, result, hints...) 41 42 return result 43 } 44 45 func (s *LocalCacheSupplier) RoleGetByNames(ctx context.Context, roleNames []string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult { 46 var foundRoles []*model.Role 47 var rolesToQuery []string 48 49 for _, roleName := range roleNames { 50 if result := s.doStandardReadCache(ctx, s.roleCache, roleName, hints...); result != nil { 51 foundRoles = append(foundRoles, result.Data.(*model.Role)) 52 } else { 53 rolesToQuery = append(rolesToQuery, roleName) 54 } 55 } 56 57 result := s.Next().RoleGetByNames(ctx, rolesToQuery, hints...) 58 59 if result.Data != nil { 60 rolesFound := result.Data.([]*model.Role) 61 for _, role := range rolesFound { 62 res := NewSupplierResult() 63 res.Data = role 64 s.doStandardAddToCache(ctx, s.roleCache, role.Name, res, hints...) 65 } 66 result.Data = append(foundRoles, result.Data.([]*model.Role)...) 67 } 68 69 return result 70 } 71 72 func (s *LocalCacheSupplier) RoleDelete(ctx context.Context, roleId string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult { 73 result := s.Next().RoleDelete(ctx, roleId, hints...) 74 75 if result.Err == nil { 76 role := result.Data.(*model.Role) 77 s.doInvalidateCacheCluster(s.roleCache, role.Name) 78 } 79 80 return result 81 } 82 83 func (s *LocalCacheSupplier) RolePermanentDeleteAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult { 84 defer s.roleCache.Purge() 85 defer s.doClearCacheCluster(s.roleCache) 86 87 return s.Next().RolePermanentDeleteAll(ctx, hints...) 88 }