Skip to content

Commit

Permalink
Merge e0fcaba into 44b7fad
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyhulen committed Feb 16, 2017
2 parents 44b7fad + e0fcaba commit e840ba8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
6 changes: 4 additions & 2 deletions api/emoji.go
Expand Up @@ -210,7 +210,7 @@ func deleteEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
}

var emoji *model.Emoji
if result := <-Srv.Store.Emoji().Get(id); result.Err != nil {
if result := <-Srv.Store.Emoji().Get(id, false); result.Err != nil {
c.Err = result.Err
return
} else {
Expand Down Expand Up @@ -268,7 +268,7 @@ func getEmojiImage(c *Context, w http.ResponseWriter, r *http.Request) {
return
}

if result := <-Srv.Store.Emoji().Get(id); result.Err != nil {
if result := <-Srv.Store.Emoji().Get(id, true); result.Err != nil {
c.Err = result.Err
return
} else {
Expand All @@ -287,6 +287,8 @@ func getEmojiImage(c *Context, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/"+imageType)
}

w.Header().Set("Cache-Control", "max-age=2592000, public")

w.Write(img)
}
}
Expand Down
36 changes: 35 additions & 1 deletion store/sql_emoji_store.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -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.(*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

Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion store/sql_emoji_store_test.go
Expand Up @@ -74,7 +74,19 @@ func TestEmojiGet(t *testing.T) {
}()

for _, emoji := range emojis {
if result := <-store.Emoji().Get(emoji.Id); result.Err != nil {
if result := <-store.Emoji().Get(emoji.Id, false); result.Err != nil {
t.Fatalf("failed to get emoji with id %v: %v", emoji.Id, result.Err)
}
}

for _, emoji := range emojis {
if result := <-store.Emoji().Get(emoji.Id, true); result.Err != nil {
t.Fatalf("failed to get emoji with id %v: %v", emoji.Id, result.Err)
}
}

for _, emoji := range emojis {
if result := <-store.Emoji().Get(emoji.Id, true); result.Err != nil {
t.Fatalf("failed to get emoji with id %v: %v", emoji.Id, result.Err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion store/store.go
Expand Up @@ -295,7 +295,7 @@ type PasswordRecoveryStore interface {

type EmojiStore interface {
Save(emoji *model.Emoji) StoreChannel
Get(id string) StoreChannel
Get(id string, allowFromCache bool) StoreChannel
GetByName(name string) StoreChannel
GetAll() StoreChannel
Delete(id string, time int64) StoreChannel
Expand Down

0 comments on commit e840ba8

Please sign in to comment.