redis_supplier_roles.go 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package store

import (
	"context"
	"fmt"

10
	"github.com/mattermost/mattermost-server/mlog"
11 12 13 14 15 16
	"github.com/mattermost/mattermost-server/model"
)

func (s *RedisSupplier) RoleSave(ctx context.Context, role *model.Role, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
	key := buildRedisKeyForRoleName(role.Name)

17 18 19 20 21
	defer func() {
		if err := s.client.Del(key).Err(); err != nil {
			mlog.Error("Redis failed to remove key " + key + " Error: " + err.Error())
		}
	}()
22

23
	return s.Next().RoleSave(ctx, role, hints...)
24 25 26 27 28 29 30 31
}

func (s *RedisSupplier) RoleGet(ctx context.Context, roleId string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
	// Roles are cached by name, as that is most commonly how they are looked up.
	// This means that no caching is supported on roles being looked up by ID.
	return s.Next().RoleGet(ctx, roleId, hints...)
}

32 33 34 35 36 37
func (s *RedisSupplier) RoleGetAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
	// Roles are cached by name, as that is most commonly how they are looked up.
	// This means that no caching is supported on roles being listed.
	return s.Next().RoleGetAll(ctx, hints...)
}

38 39 40 41 42 43
func (s *RedisSupplier) RoleGetByName(ctx context.Context, name string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
	key := buildRedisKeyForRoleName(name)

	var role *model.Role
	found, err := s.load(key, &role)
	if err != nil {
44
		mlog.Error("Redis encountered an error on read: " + err.Error())
45 46 47 48 49 50 51 52 53 54
	} else if found {
		result := NewSupplierResult()
		result.Data = role
		return result
	}

	result := s.Next().RoleGetByName(ctx, name, hints...)

	if result.Err == nil {
		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 60 61 62 63 64 65 66 67 68 69 70 71 72 73
		}
	}

	return result
}

func (s *RedisSupplier) RoleGetByNames(ctx context.Context, roleNames []string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
	var foundRoles []*model.Role
	var rolesToQuery []string

	for _, roleName := range roleNames {
		var role *model.Role
		found, err := s.load(buildRedisKeyForRoleName(roleName), &role)
		if err == nil && found {
			foundRoles = append(foundRoles, role)
		} else {
			rolesToQuery = append(rolesToQuery, roleName)
			if err != nil {
74
				mlog.Error("Redis encountered an error on read: " + err.Error())
75 76 77 78 79 80 81 82 83 84
			}
		}
	}

	result := s.Next().RoleGetByNames(ctx, rolesToQuery, hints...)

	if result.Err == nil {
		rolesFound := result.Data.([]*model.Role)
		for _, role := range rolesFound {
			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 89 90 91 92 93
			}
		}
		result.Data = append(foundRoles, result.Data.([]*model.Role)...)
	}

	return result
}

94 95 96 97
func (s *RedisSupplier) RoleDelete(ctx context.Context, roleId string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
	result := s.Next().RoleGet(ctx, roleId, hints...)

	if result.Err == nil {
98 99 100
		defer func() {
			role := result.Data.(*model.Role)
			key := buildRedisKeyForRoleName(role.Name)
101

102 103 104 105
			if err := s.client.Del(key).Err(); err != nil {
				mlog.Error("Redis failed to remove key " + key + " Error: " + err.Error())
			}
		}()
106 107
	}

108
	return s.Next().RoleDelete(ctx, roleId, hints...)
109 110
}

111
func (s *RedisSupplier) RolePermanentDeleteAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
112 113 114 115 116 117 118
	defer func() {
		if keys, err := s.client.Keys("roles:*").Result(); err != nil {
			mlog.Error("Redis encountered an error on read: " + err.Error())
		} else {
			if err := s.client.Del(keys...).Err(); err != nil {
				mlog.Error("Redis encountered an error on delete: " + err.Error())
			}
119
		}
120
	}()
121

122
	return s.Next().RolePermanentDeleteAll(ctx, hints...)
123 124
}

125 126 127
func buildRedisKeyForRoleName(roleName string) string {
	return fmt.Sprintf("roles:%s", roleName)
}