github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/redis_supplier_roles.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package store
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"github.com/mattermost/mattermost-server/mlog"
    11  	"github.com/mattermost/mattermost-server/model"
    12  )
    13  
    14  func (s *RedisSupplier) RoleSave(ctx context.Context, role *model.Role, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    15  	key := buildRedisKeyForRoleName(role.Name)
    16  
    17  	defer func() {
    18  		if err := s.client.Del(key).Err(); err != nil {
    19  			mlog.Error("Redis failed to remove key " + key + " Error: " + err.Error())
    20  		}
    21  	}()
    22  
    23  	return s.Next().RoleSave(ctx, role, hints...)
    24  }
    25  
    26  func (s *RedisSupplier) RoleGet(ctx context.Context, roleId string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    27  	// Roles are cached by name, as that is most commonly how they are looked up.
    28  	// This means that no caching is supported on roles being looked up by ID.
    29  	return s.Next().RoleGet(ctx, roleId, hints...)
    30  }
    31  
    32  func (s *RedisSupplier) RoleGetAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    33  	// Roles are cached by name, as that is most commonly how they are looked up.
    34  	// This means that no caching is supported on roles being listed.
    35  	return s.Next().RoleGetAll(ctx, hints...)
    36  }
    37  
    38  func (s *RedisSupplier) RoleGetByName(ctx context.Context, name string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    39  	key := buildRedisKeyForRoleName(name)
    40  
    41  	var role *model.Role
    42  	found, err := s.load(key, &role)
    43  	if err != nil {
    44  		mlog.Error("Redis encountered an error on read: " + err.Error())
    45  	} else if found {
    46  		result := NewSupplierResult()
    47  		result.Data = role
    48  		return result
    49  	}
    50  
    51  	result := s.Next().RoleGetByName(ctx, name, hints...)
    52  
    53  	if result.Err == nil {
    54  		if err := s.save(key, result.Data, REDIS_EXPIRY_TIME); err != nil {
    55  			mlog.Error("Redis encountered and error on write: " + err.Error())
    56  		}
    57  	}
    58  
    59  	return result
    60  }
    61  
    62  func (s *RedisSupplier) RoleGetByNames(ctx context.Context, roleNames []string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    63  	var foundRoles []*model.Role
    64  	var rolesToQuery []string
    65  
    66  	for _, roleName := range roleNames {
    67  		var role *model.Role
    68  		found, err := s.load(buildRedisKeyForRoleName(roleName), &role)
    69  		if err == nil && found {
    70  			foundRoles = append(foundRoles, role)
    71  		} else {
    72  			rolesToQuery = append(rolesToQuery, roleName)
    73  			if err != nil {
    74  				mlog.Error("Redis encountered an error on read: " + err.Error())
    75  			}
    76  		}
    77  	}
    78  
    79  	result := s.Next().RoleGetByNames(ctx, rolesToQuery, hints...)
    80  
    81  	if result.Err == nil {
    82  		rolesFound := result.Data.([]*model.Role)
    83  		for _, role := range rolesFound {
    84  			if err := s.save(buildRedisKeyForRoleName(role.Name), role, REDIS_EXPIRY_TIME); err != nil {
    85  				mlog.Error("Redis encountered and error on write: " + err.Error())
    86  			}
    87  		}
    88  		result.Data = append(foundRoles, result.Data.([]*model.Role)...)
    89  	}
    90  
    91  	return result
    92  }
    93  
    94  func (s *RedisSupplier) RoleDelete(ctx context.Context, roleId string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    95  	result := s.Next().RoleGet(ctx, roleId, hints...)
    96  
    97  	if result.Err == nil {
    98  		defer func() {
    99  			role := result.Data.(*model.Role)
   100  			key := buildRedisKeyForRoleName(role.Name)
   101  
   102  			if err := s.client.Del(key).Err(); err != nil {
   103  				mlog.Error("Redis failed to remove key " + key + " Error: " + err.Error())
   104  			}
   105  		}()
   106  	}
   107  
   108  	return s.Next().RoleDelete(ctx, roleId, hints...)
   109  }
   110  
   111  func (s *RedisSupplier) RolePermanentDeleteAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
   112  	defer func() {
   113  		if keys, err := s.client.Keys("roles:*").Result(); err != nil {
   114  			mlog.Error("Redis encountered an error on read: " + err.Error())
   115  		} else {
   116  			if err := s.client.Del(keys...).Err(); err != nil {
   117  				mlog.Error("Redis encountered an error on delete: " + err.Error())
   118  			}
   119  		}
   120  	}()
   121  
   122  	return s.Next().RolePermanentDeleteAll(ctx, hints...)
   123  }
   124  
   125  func buildRedisKeyForRoleName(roleName string) string {
   126  	return fmt.Sprintf("roles:%s", roleName)
   127  }