summaryrefslogtreecommitdiffstats
path: root/store/local_cache_supplier_roles.go
blob: 7c82f60eb1cdf35a1449d29cdce7eeb27ccf0b59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package store

import (
	"context"

	"github.com/mattermost/mattermost-server/model"
)

func (s *LocalCacheSupplier) handleClusterInvalidateRole(msg *model.ClusterMessage) {
	if msg.Data == CLEAR_CACHE_MESSAGE_DATA {
		s.roleCache.Purge()
	} else {
		s.roleCache.Remove(msg.Data)
	}
}

func (s *LocalCacheSupplier) RoleSave(ctx context.Context, role *model.Role, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
	if len(role.Id) != 0 {
		defer s.doInvalidateCacheCluster(s.roleCache, role.Name)
	}
	return s.Next().RoleSave(ctx, role, hints...)
}

func (s *LocalCacheSupplier) 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...)
}

func (s *LocalCacheSupplier) RoleGetByName(ctx context.Context, name string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
	if result := s.doStandardReadCache(ctx, s.roleCache, name, hints...); result != nil {
		return result
	}

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

	s.doStandardAddToCache(ctx, s.roleCache, name, result, hints...)

	return result
}

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

	for _, roleName := range roleNames {
		if result := s.doStandardReadCache(ctx, s.roleCache, roleName, hints...); result != nil {
			foundRoles = append(foundRoles, result.Data.(*model.Role))
		} else {
			rolesToQuery = append(rolesToQuery, roleName)
		}
	}

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

	if result.Data != nil {
		rolesFound := result.Data.([]*model.Role)
		for _, role := range rolesFound {
			res := NewSupplierResult()
			res.Data = role
			s.doStandardAddToCache(ctx, s.roleCache, role.Name, res, hints...)
		}
		result.Data = append(foundRoles, result.Data.([]*model.Role)...)
	}

	return result
}

func (s *LocalCacheSupplier) RolePermanentDeleteAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
	defer s.roleCache.Purge()
	defer s.doClearCacheCluster(s.roleCache)

	return s.Next().RolePermanentDeleteAll(ctx, hints...)
}