diff options
author | Christopher Speller <crspeller@gmail.com> | 2017-03-07 09:37:00 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-07 09:37:00 -0500 |
commit | 33b8b72a026c54f121f1d0f0370d9ad2e6220a61 (patch) | |
tree | 005fe432e44b208a980f2b28a647e2ea48b6ed44 /store | |
parent | 115acb1fb1cef5dfe222af7f5ae02b5d1a9d9d4f (diff) | |
download | chat-33b8b72a026c54f121f1d0f0370d9ad2e6220a61.tar.gz chat-33b8b72a026c54f121f1d0f0370d9ad2e6220a61.tar.bz2 chat-33b8b72a026c54f121f1d0f0370d9ad2e6220a61.zip |
Adding index and cache to reactinos store (#5654)
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_reaction_store.go | 43 | ||||
-rw-r--r-- | store/sql_reaction_store_test.go | 38 | ||||
-rw-r--r-- | store/store.go | 4 |
3 files changed, 77 insertions, 8 deletions
diff --git a/store/sql_reaction_store.go b/store/sql_reaction_store.go index 7bd063a15..076710efc 100644 --- a/store/sql_reaction_store.go +++ b/store/sql_reaction_store.go @@ -4,6 +4,7 @@ package store import ( + "github.com/mattermost/platform/einterfaces" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" @@ -11,6 +12,13 @@ import ( "github.com/go-gorp/gorp" ) +const ( + REACTION_CACHE_SIZE = 20000 + REACTION_CACHE_SEC = 1800 // 30 minutes +) + +var reactionCache *utils.Cache = utils.NewLru(REACTION_CACHE_SIZE) + type SqlReactionStore struct { *SqlStore } @@ -30,6 +38,8 @@ func NewSqlReactionStore(sqlStore *SqlStore) ReactionStore { func (s SqlReactionStore) CreateIndexesIfNotExists() { s.CreateIndexIfNotExists("idx_reactions_post_id", "Reactions", "PostId") + s.CreateIndexIfNotExists("idx_reactions_user_id", "Reactions", "UserId") + s.CreateIndexIfNotExists("idx_reactions_emoji_name", "Reactions", "EmojiName") } func (s SqlReactionStore) Save(reaction *model.Reaction) StoreChannel { @@ -149,11 +159,40 @@ func updatePostForReactions(transaction *gorp.Transaction, postId string) error return err } -func (s SqlReactionStore) GetForPost(postId string) StoreChannel { +func (s SqlReactionStore) InvalidateCacheForPost(postId string) { + reactionCache.Remove(postId) +} + +func (s SqlReactionStore) InvalidateCache() { + reactionCache.Purge() +} + +func (s SqlReactionStore) GetForPost(postId string, allowFromCache bool) StoreChannel { storeChannel := make(StoreChannel) go func() { result := StoreResult{} + metrics := einterfaces.GetMetricsInterface() + + if allowFromCache { + if cacheItem, ok := reactionCache.Get(postId); ok { + if metrics != nil { + metrics.IncrementMemCacheHitCounter("Reactions") + } + result.Data = cacheItem.([]*model.Reaction) + storeChannel <- result + close(storeChannel) + return + } else { + if metrics != nil { + metrics.IncrementMemCacheMissCounter("Reactions") + } + } + } else { + if metrics != nil { + metrics.IncrementMemCacheMissCounter("Reactions") + } + } var reactions []*model.Reaction @@ -169,6 +208,8 @@ func (s SqlReactionStore) GetForPost(postId string) StoreChannel { result.Err = model.NewLocAppError("SqlReactionStore.GetForPost", "store.sql_reaction.get_for_post.app_error", nil, "") } else { result.Data = reactions + + reactionCache.AddWithExpiresInSecs(postId, reactions, REACTION_CACHE_SEC) } storeChannel <- result diff --git a/store/sql_reaction_store_test.go b/store/sql_reaction_store_test.go index 5a1cb2d67..01ce14e65 100644 --- a/store/sql_reaction_store_test.go +++ b/store/sql_reaction_store_test.go @@ -4,8 +4,9 @@ package store import ( - "github.com/mattermost/platform/model" "testing" + + "github.com/mattermost/platform/model" ) func TestReactionSave(t *testing.T) { @@ -108,7 +109,7 @@ func TestReactionDelete(t *testing.T) { t.Fatal(result.Err) } - if result := <-store.Reaction().GetForPost(post.Id); result.Err != nil { + if result := <-store.Reaction().GetForPost(post.Id, false); result.Err != nil { t.Fatal(result.Err) } else if len(result.Data.([]*model.Reaction)) != 0 { t.Fatal("should've deleted reaction") @@ -155,7 +156,32 @@ func TestReactionGetForPost(t *testing.T) { Must(store.Reaction().Save(reaction)) } - if result := <-store.Reaction().GetForPost(postId); result.Err != nil { + if result := <-store.Reaction().GetForPost(postId, false); result.Err != nil { + t.Fatal(result.Err) + } else if returned := result.Data.([]*model.Reaction); len(returned) != 3 { + t.Fatal("should've returned 3 reactions") + } else { + for _, reaction := range reactions { + found := false + + for _, returnedReaction := range returned { + if returnedReaction.UserId == reaction.UserId && returnedReaction.PostId == reaction.PostId && + returnedReaction.EmojiName == reaction.EmojiName { + found = true + break + } + } + + if !found && reaction.PostId == postId { + t.Fatalf("should've returned reaction for post %v", reaction) + } else if found && reaction.PostId != postId { + t.Fatal("shouldn't have returned reaction for another post") + } + } + } + + // Should return cached item + if result := <-store.Reaction().GetForPost(postId, true); result.Err != nil { t.Fatal(result.Err) } else if returned := result.Data.([]*model.Reaction); len(returned) != 3 { t.Fatal("should've returned 3 reactions") @@ -237,7 +263,7 @@ func TestReactionDeleteAllWithEmojiName(t *testing.T) { } // check that the reactions were deleted - if returned := Must(store.Reaction().GetForPost(post.Id)).([]*model.Reaction); len(returned) != 1 { + if returned := Must(store.Reaction().GetForPost(post.Id, false)).([]*model.Reaction); len(returned) != 1 { t.Fatal("should've only removed reactions with emoji name") } else { for _, reaction := range returned { @@ -247,11 +273,11 @@ func TestReactionDeleteAllWithEmojiName(t *testing.T) { } } - if returned := Must(store.Reaction().GetForPost(post2.Id)).([]*model.Reaction); len(returned) != 1 { + if returned := Must(store.Reaction().GetForPost(post2.Id, false)).([]*model.Reaction); len(returned) != 1 { t.Fatal("should've only removed reactions with emoji name") } - if returned := Must(store.Reaction().GetForPost(post3.Id)).([]*model.Reaction); len(returned) != 0 { + if returned := Must(store.Reaction().GetForPost(post3.Id, false)).([]*model.Reaction); len(returned) != 0 { t.Fatal("should've only removed reactions with emoji name") } diff --git a/store/store.go b/store/store.go index a436f9ee7..7aa903f6f 100644 --- a/store/store.go +++ b/store/store.go @@ -348,6 +348,8 @@ type FileInfoStore interface { type ReactionStore interface { Save(reaction *model.Reaction) StoreChannel Delete(reaction *model.Reaction) StoreChannel - GetForPost(postId string) StoreChannel + InvalidateCacheForPost(postId string) + InvalidateCache() + GetForPost(postId string, allowFromCache bool) StoreChannel DeleteAllWithEmojiName(emojiName string) StoreChannel } |