summaryrefslogtreecommitdiffstats
path: root/store/sql_emoji_store.go
diff options
context:
space:
mode:
authorCorey Hulen <corey@hulen.com>2017-02-15 19:25:33 -0500
committerJoram Wilander <jwawilander@gmail.com>2017-02-15 19:25:33 -0500
commit01a8114aa3d0a349e3b389ecc1e979c4123dee75 (patch)
tree741f39ba79af1b9abd328808fcb06267f8be65cc /store/sql_emoji_store.go
parent727d067fcee4e97497152ebf8ca46a5ad254422e (diff)
downloadchat-01a8114aa3d0a349e3b389ecc1e979c4123dee75.tar.gz
chat-01a8114aa3d0a349e3b389ecc1e979c4123dee75.tar.bz2
chat-01a8114aa3d0a349e3b389ecc1e979c4123dee75.zip
Adding emoji caching (#5433)
Diffstat (limited to 'store/sql_emoji_store.go')
-rw-r--r--store/sql_emoji_store.go36
1 files changed, 35 insertions, 1 deletions
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