github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/store/local_cache_supplier_roles.go (about) 1 // Copyright (c) 2017-present Xenia, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package store 5 6 import ( 7 "context" 8 9 "github.com/xzl8028/xenia-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) (*model.Role, *model.AppError) { 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) (*model.Role, *model.AppError) { 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) ([]*model.Role, *model.AppError) { 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) (*model.Role, *model.AppError) { 40 if result := s.doStandardReadCache(ctx, s.roleCache, name, hints...); result != nil { 41 return result.Data.(*model.Role), nil 42 } 43 44 role, err := s.Next().RoleGetByName(ctx, name, hints...) 45 if err != nil { 46 return nil, err 47 } 48 49 result := NewSupplierResult() 50 result.Data = role 51 s.doStandardAddToCache(ctx, s.roleCache, name, result, hints...) 52 53 return role, nil 54 } 55 56 func (s *LocalCacheSupplier) RoleGetByNames(ctx context.Context, roleNames []string, hints ...LayeredStoreHint) ([]*model.Role, *model.AppError) { 57 var foundRoles []*model.Role 58 var rolesToQuery []string 59 60 for _, roleName := range roleNames { 61 if result := s.doStandardReadCache(ctx, s.roleCache, roleName, hints...); result != nil { 62 foundRoles = append(foundRoles, result.Data.(*model.Role)) 63 } else { 64 rolesToQuery = append(rolesToQuery, roleName) 65 } 66 } 67 68 rolesFound, err := s.Next().RoleGetByNames(ctx, rolesToQuery, hints...) 69 70 for _, role := range rolesFound { 71 res := NewSupplierResult() 72 res.Data = role 73 s.doStandardAddToCache(ctx, s.roleCache, role.Name, res, hints...) 74 } 75 foundRoles = append(foundRoles, rolesFound...) 76 77 return foundRoles, err 78 } 79 80 func (s *LocalCacheSupplier) RoleDelete(ctx context.Context, roleId string, hints ...LayeredStoreHint) (*model.Role, *model.AppError) { 81 role, err := s.Next().RoleDelete(ctx, roleId, hints...) 82 if err != nil { 83 return nil, err 84 } 85 86 s.doInvalidateCacheCluster(s.roleCache, role.Name) 87 88 return role, nil 89 } 90 91 func (s *LocalCacheSupplier) RolePermanentDeleteAll(ctx context.Context, hints ...LayeredStoreHint) *model.AppError { 92 defer s.roleCache.Purge() 93 defer s.doClearCacheCluster(s.roleCache) 94 95 return s.Next().RolePermanentDeleteAll(ctx, hints...) 96 }