forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrating team store cache to cache layer (mattermost#13075)
Automatic Merge
- Loading branch information
1 parent
e2a2a1a
commit e034117
Showing
7 changed files
with
175 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. | ||
// See License.txt for license information. | ||
|
||
package localcachelayer | ||
|
||
import ( | ||
"github.com/mattermost/mattermost-server/v5/model" | ||
"github.com/mattermost/mattermost-server/v5/store" | ||
) | ||
|
||
type LocalCacheTeamStore struct { | ||
store.TeamStore | ||
rootStore *LocalCacheStore | ||
} | ||
|
||
func (s *LocalCacheTeamStore) handleClusterInvalidateTeam(msg *model.ClusterMessage) { | ||
if msg.Data == CLEAR_CACHE_MESSAGE_DATA { | ||
s.rootStore.teamAllTeamIdsForUserCache.Purge() | ||
} else { | ||
s.rootStore.teamAllTeamIdsForUserCache.Remove(msg.Data) | ||
} | ||
} | ||
|
||
func (s LocalCacheTeamStore) ClearCaches() { | ||
s.rootStore.teamAllTeamIdsForUserCache.Purge() | ||
if s.rootStore.metrics != nil { | ||
s.rootStore.metrics.IncrementMemCacheInvalidationCounter("All Team Ids for User - Purge") | ||
} | ||
} | ||
|
||
func (s LocalCacheTeamStore) InvalidateAllTeamIdsForUser(userId string) { | ||
s.rootStore.doInvalidateCacheCluster(s.rootStore.teamAllTeamIdsForUserCache, userId) | ||
if s.rootStore.metrics != nil { | ||
s.rootStore.metrics.IncrementMemCacheInvalidationCounter("All Team Ids for User - Remove by UserId") | ||
} | ||
} | ||
|
||
func (s LocalCacheTeamStore) GetUserTeamIds(userID string, allowFromCache bool) ([]string, *model.AppError) { | ||
if !allowFromCache { | ||
return s.TeamStore.GetUserTeamIds(userID, allowFromCache) | ||
} | ||
|
||
if userTeamIds := s.rootStore.doStandardReadCache(s.rootStore.teamAllTeamIdsForUserCache, userID); userTeamIds != nil { | ||
return userTeamIds.([]string), nil | ||
} | ||
|
||
userTeamIds, err := s.TeamStore.GetUserTeamIds(userID, allowFromCache) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(userTeamIds) > 0 { | ||
s.rootStore.doStandardAddToCache(s.rootStore.teamAllTeamIdsForUserCache, userID, userTeamIds) | ||
} | ||
|
||
return userTeamIds, nil | ||
} | ||
|
||
func (s LocalCacheTeamStore) Update(team *model.Team) (*model.Team, *model.AppError) { | ||
var oldTeam *model.Team | ||
var err *model.AppError | ||
if team.DeleteAt != 0 { | ||
oldTeam, err = s.TeamStore.Get(team.Id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
tm, err := s.TeamStore.Update(team) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if oldTeam != nil && oldTeam.DeleteAt == 0 { | ||
s.rootStore.doClearCacheCluster(s.rootStore.teamAllTeamIdsForUserCache) | ||
} | ||
|
||
return tm, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. | ||
// See License.txt for license information. | ||
|
||
package localcachelayer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mattermost/mattermost-server/v5/store/storetest" | ||
"github.com/mattermost/mattermost-server/v5/store/storetest/mocks" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTeamStore(t *testing.T) { | ||
StoreTest(t, storetest.TestTeamStore) | ||
} | ||
|
||
func TestTeamStoreCache(t *testing.T) { | ||
fakeUserId := "123" | ||
fakeUserTeamIds := []string{"1", "2", "3"} | ||
|
||
t.Run("first call not cached, second cached and returning same data", func(t *testing.T) { | ||
mockStore := getMockStore() | ||
cachedStore := NewLocalCacheLayer(mockStore, nil, nil) | ||
|
||
gotUserTeamIds, err := cachedStore.Team().GetUserTeamIds(fakeUserId, true) | ||
require.Nil(t, err) | ||
assert.Equal(t, fakeUserTeamIds, gotUserTeamIds) | ||
mockStore.Team().(*mocks.TeamStore).AssertNumberOfCalls(t, "GetUserTeamIds", 1) | ||
|
||
gotUserTeamIds, err = cachedStore.Team().GetUserTeamIds(fakeUserId, true) | ||
require.Nil(t, err) | ||
assert.Equal(t, fakeUserTeamIds, gotUserTeamIds) | ||
mockStore.Team().(*mocks.TeamStore).AssertNumberOfCalls(t, "GetUserTeamIds", 1) | ||
}) | ||
|
||
t.Run("first call not cached, second force no cached", func(t *testing.T) { | ||
mockStore := getMockStore() | ||
cachedStore := NewLocalCacheLayer(mockStore, nil, nil) | ||
|
||
gotUserTeamIds, err := cachedStore.Team().GetUserTeamIds(fakeUserId, true) | ||
require.Nil(t, err) | ||
assert.Equal(t, fakeUserTeamIds, gotUserTeamIds) | ||
mockStore.Team().(*mocks.TeamStore).AssertNumberOfCalls(t, "GetUserTeamIds", 1) | ||
|
||
gotUserTeamIds, err = cachedStore.Team().GetUserTeamIds(fakeUserId, false) | ||
require.Nil(t, err) | ||
assert.Equal(t, fakeUserTeamIds, gotUserTeamIds) | ||
mockStore.Team().(*mocks.TeamStore).AssertNumberOfCalls(t, "GetUserTeamIds", 2) | ||
}) | ||
|
||
t.Run("first call not cached, invalidate, and then not cached again", func(t *testing.T) { | ||
mockStore := getMockStore() | ||
cachedStore := NewLocalCacheLayer(mockStore, nil, nil) | ||
|
||
gotUserTeamIds, err := cachedStore.Team().GetUserTeamIds(fakeUserId, true) | ||
require.Nil(t, err) | ||
assert.Equal(t, fakeUserTeamIds, gotUserTeamIds) | ||
mockStore.Team().(*mocks.TeamStore).AssertNumberOfCalls(t, "GetUserTeamIds", 1) | ||
|
||
cachedStore.Team().InvalidateAllTeamIdsForUser(fakeUserId) | ||
|
||
gotUserTeamIds, err = cachedStore.Team().GetUserTeamIds(fakeUserId, true) | ||
require.Nil(t, err) | ||
assert.Equal(t, fakeUserTeamIds, gotUserTeamIds) | ||
mockStore.Team().(*mocks.TeamStore).AssertNumberOfCalls(t, "GetUserTeamIds", 2) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters