From 01a8114aa3d0a349e3b389ecc1e979c4123dee75 Mon Sep 17 00:00:00 2001 From: Corey Hulen Date: Wed, 15 Feb 2017 19:25:33 -0500 Subject: Adding emoji caching (#5433) --- store/sql_emoji_store.go | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'store/sql_emoji_store.go') diff --git a/store/sql_emoji_store.go b/store/sql_emoji_store.go index f9218d8d7..5aad725f9 100644 --- a/store/sql_emoji_store.go +++ b/store/sql_emoji_store.go @@ -4,9 +4,18 @@ package store import ( + "github.com/mattermost/platform/einterfaces" "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" ) +const ( + EMOJI_CACHE_SIZE = 5000 + EMOJI_CACHE_SEC = 1800 // 60 mins +) + +var emojiCache *utils.Cache = utils.NewLru(EMOJI_CACHE_SIZE) + type SqlEmojiStore struct { *SqlStore } @@ -58,11 +67,32 @@ func (es SqlEmojiStore) Save(emoji *model.Emoji) StoreChannel { return storeChannel } -func (es SqlEmojiStore) Get(id string) StoreChannel { +func (es SqlEmojiStore) Get(id string, allowFromCache bool) StoreChannel { storeChannel := make(StoreChannel, 1) go func() { result := StoreResult{} + metrics := einterfaces.GetMetricsInterface() + + if allowFromCache { + if cacheItem, ok := emojiCache.Get(id); ok { + if metrics != nil { + metrics.IncrementMemCacheHitCounter("Emoji") + } + result.Data = cacheItem.(map[string]*model.Emoji) + storeChannel <- result + close(storeChannel) + return + } else { + if metrics != nil { + metrics.IncrementMemCacheMissCounter("Emoji") + } + } + } else { + if metrics != nil { + metrics.IncrementMemCacheMissCounter("Emoji") + } + } var emoji *model.Emoji @@ -77,6 +107,10 @@ func (es SqlEmojiStore) Get(id string) StoreChannel { result.Err = model.NewLocAppError("SqlEmojiStore.Get", "store.sql_emoji.get.app_error", nil, "id="+id+", "+err.Error()) } else { result.Data = emoji + + if allowFromCache { + emojiCache.AddWithExpiresInSecs(id, emoji, EMOJI_CACHE_SEC) + } } storeChannel <- result -- cgit v1.2.3-1-g7c22