github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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  	if err := s.client.Del(key).Err(); err != nil {
    18  		mlog.Error("Redis failed to remove key " + key + " Error: " + err.Error())
    19  	}
    20  
    21  	return s.Next().RoleSave(ctx, role, hints...)
    22  }
    23  
    24  func (s *RedisSupplier) RoleGet(ctx context.Context, roleId string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    25  	// Roles are cached by name, as that is most commonly how they are looked up.
    26  	// This means that no caching is supported on roles being looked up by ID.
    27  	return s.Next().RoleGet(ctx, roleId, hints...)
    28  }
    29  
    30  func (s *RedisSupplier) RoleGetByName(ctx context.Context, name string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    31  	key := buildRedisKeyForRoleName(name)
    32  
    33  	var role *model.Role
    34  	found, err := s.load(key, &role)
    35  	if err != nil {
    36  		mlog.Error("Redis encountered an error on read: " + err.Error())
    37  	} else if found {
    38  		result := NewSupplierResult()
    39  		result.Data = role
    40  		return result
    41  	}
    42  
    43  	result := s.Next().RoleGetByName(ctx, name, hints...)
    44  
    45  	if result.Err == nil {
    46  		if err := s.save(key, result.Data, REDIS_EXPIRY_TIME); err != nil {
    47  			mlog.Error("Redis encountered and error on write: " + err.Error())
    48  		}
    49  	}
    50  
    51  	return result
    52  }
    53  
    54  func (s *RedisSupplier) RoleGetByNames(ctx context.Context, roleNames []string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    55  	var foundRoles []*model.Role
    56  	var rolesToQuery []string
    57  
    58  	for _, roleName := range roleNames {
    59  		var role *model.Role
    60  		found, err := s.load(buildRedisKeyForRoleName(roleName), &role)
    61  		if err == nil && found {
    62  			foundRoles = append(foundRoles, role)
    63  		} else {
    64  			rolesToQuery = append(rolesToQuery, roleName)
    65  			if err != nil {
    66  				mlog.Error("Redis encountered an error on read: " + err.Error())
    67  			}
    68  		}
    69  	}
    70  
    71  	result := s.Next().RoleGetByNames(ctx, rolesToQuery, hints...)
    72  
    73  	if result.Err == nil {
    74  		rolesFound := result.Data.([]*model.Role)
    75  		for _, role := range rolesFound {
    76  			if err := s.save(buildRedisKeyForRoleName(role.Name), role, REDIS_EXPIRY_TIME); err != nil {
    77  				mlog.Error("Redis encountered and error on write: " + err.Error())
    78  			}
    79  		}
    80  		result.Data = append(foundRoles, result.Data.([]*model.Role)...)
    81  	}
    82  
    83  	return result
    84  }
    85  
    86  func (s *RedisSupplier) RoleDelete(ctx context.Context, roleId string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    87  	result := s.Next().RoleGet(ctx, roleId, hints...)
    88  
    89  	if result.Err == nil {
    90  		role := result.Data.(*model.Role)
    91  		key := buildRedisKeyForRoleName(role.Name)
    92  
    93  		if err := s.client.Del(key).Err(); err != nil {
    94  			mlog.Error("Redis failed to remove key " + key + " Error: " + err.Error())
    95  		}
    96  	}
    97  
    98  	return result
    99  }
   100  
   101  func (s *RedisSupplier) RolePermanentDeleteAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
   102  	defer func() {
   103  		if keys, err := s.client.Keys("roles:*").Result(); err != nil {
   104  			mlog.Error("Redis encountered an error on read: " + err.Error())
   105  		} else {
   106  			if err := s.client.Del(keys...).Err(); err != nil {
   107  				mlog.Error("Redis encountered an error on delete: " + err.Error())
   108  			}
   109  		}
   110  	}()
   111  
   112  	return s.Next().RolePermanentDeleteAll(ctx, hints...)
   113  }
   114  
   115  func buildRedisKeyForRoleName(roleName string) string {
   116  	return fmt.Sprintf("roles:%s", roleName)
   117  }