diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-03-07 16:05:01 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-07 16:05:01 -0500 |
commit | 83291e4c8fd1c2d234ac9f4254af849bed64cabf (patch) | |
tree | 7457bacc4b91c35fa100d6b77fed1f411b36dcf5 /store/sql_reaction_store.go | |
parent | e753d3dc3c8aeb6d4a8acc48ec1fa9ead10eb6ab (diff) | |
download | chat-83291e4c8fd1c2d234ac9f4254af849bed64cabf.tar.gz chat-83291e4c8fd1c2d234ac9f4254af849bed64cabf.tar.bz2 chat-83291e4c8fd1c2d234ac9f4254af849bed64cabf.zip |
Merge 3.7 changes into master (#5679)
* Save schema version for 3.7 (#5675)
* Adding index and cache to reactinos store (#5654)
* Fix badge count for push notifications (#5672)
Diffstat (limited to 'store/sql_reaction_store.go')
-rw-r--r-- | store/sql_reaction_store.go | 43 |
1 files changed, 42 insertions, 1 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 |