MM-53669: Use the cache layer for EmojiStore.GetMultipleByName (#24030)

There was already a cache present for emoji names. But we weren't using
it for the GetMultipleByName method. Now we implement that method
to look up the cache for every emoji name passed.

Secondly, a bigger problem was that we were making the DB call for system
emojis as well. Since system emojis aren't stored in the DB, it would
fall through the cache layer and always make a redundant DB call. In the
profiles, this should up as taking 16% of the total time to serve
a getPostsForChannel API endpoint.

We fix this by filtering the emojis to only custom emojis
before making the call.

https://mattermost.atlassian.net/browse/MM-53669

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2023-07-18 20:27:48 +05:30
коммит произвёл GitHub
родитель b247c6251f
Коммит 3c31629813
13 изменённых файлов: 154 добавлений и 31 удалений

Просмотреть файл

@@ -191,7 +191,7 @@ func (a *App) GetEmoji(c request.CTX, emojiId string) (*model.Emoji, *model.AppE
return nil, model.NewAppError("GetEmoji", "api.emoji.storage.app_error", nil, "", http.StatusForbidden)
}
emoji, err := a.Srv().Store().Emoji().Get(context.Background(), emojiId, true)
emoji, err := a.Srv().Store().Emoji().Get(c.Context(), emojiId, true)
if err != nil {
var nfErr *store.ErrNotFound
switch {
@@ -214,7 +214,7 @@ func (a *App) GetEmojiByName(c request.CTX, emojiName string) (*model.Emoji, *mo
return nil, model.NewAppError("GetEmojiByName", "api.emoji.storage.app_error", nil, "", http.StatusForbidden)
}
emoji, err := a.Srv().Store().Emoji().GetByName(context.Background(), emojiName, true)
emoji, err := a.Srv().Store().Emoji().GetByName(c.Context(), emojiName, true)
if err != nil {
var nfErr *store.ErrNotFound
switch {
@@ -233,7 +233,21 @@ func (a *App) GetMultipleEmojiByName(c request.CTX, names []string) ([]*model.Em
return nil, model.NewAppError("GetMultipleEmojiByName", "api.emoji.disabled.app_error", nil, "", http.StatusForbidden)
}
emoji, err := a.Srv().Store().Emoji().GetMultipleByName(names)
// Filtering out system emojis
i := 0
for _, n := range names {
if _, ok := model.GetSystemEmojiId(n); !ok {
names[i] = n
i++
}
}
names = names[:i]
if len(names) == 0 {
return []*model.Emoji{}, nil
}
emoji, err := a.Srv().Store().Emoji().GetMultipleByName(c.Context(), names)
if err != nil {
return nil, model.NewAppError("GetMultipleEmojiByName", "app.emoji.get_by_name.app_error", nil, fmt.Sprintf("names=%v, %v", names, err.Error()), http.StatusInternalServerError)
}
@@ -242,7 +256,7 @@ func (a *App) GetMultipleEmojiByName(c request.CTX, names []string) ([]*model.Em
}
func (a *App) GetEmojiImage(c request.CTX, emojiId string) ([]byte, string, *model.AppError) {
_, storeErr := a.Srv().Store().Emoji().Get(context.Background(), emojiId, true)
_, storeErr := a.Srv().Store().Emoji().Get(c.Context(), emojiId, true)
if storeErr != nil {
var nfErr *store.ErrNotFound
switch {

29
server/channels/app/emoji_test.go Обычный файл
Просмотреть файл

@@ -0,0 +1,29 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"testing"
"github.com/mattermost/mattermost/server/public/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetMultipleEmojiByName(t *testing.T) {
// The fact that we use mock store ensures that
// the call to the DB does not happen. If it did, we would have needed
// to provide the mock explicitly.
th := SetupWithStoreMock(t)
defer th.TearDown()
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.EnableCustomEmoji = true
})
// Ensure it returns empty for system emojis
emojis, appErr := th.App.GetMultipleEmojiByName(th.Context, []string{"+1"})
require.Nil(t, appErr)
assert.Empty(t, emojis)
}

Просмотреть файл

@@ -438,7 +438,6 @@ func (a *App) getCustomEmojisForPost(c request.CTX, post *model.Post, reactions
}
names := getEmojiNamesForPost(post, reactions)
if len(names) == 0 {
return []*model.Emoji{}, nil
}