From 09b49c26ddfdb20ced61e7dfd4192e750ce40449 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Mon, 31 Jul 2017 08:15:23 -0700 Subject: PLT-5308 Caching layer part 2 (#6973) * Adding Reaction store cache layer example * Implementing reaction store in new caching system. * Redis for reaction store * Adding redis library * Adding invalidation for DeleteAllWithEmojiName and other minor enhancements --- store/layered_store.go | 70 +++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 32 deletions(-) (limited to 'store/layered_store.go') diff --git a/store/layered_store.go b/store/layered_store.go index ab9859c80..3d3f941e8 100644 --- a/store/layered_store.go +++ b/store/layered_store.go @@ -6,38 +6,54 @@ package store import ( "context" + l4g "github.com/alecthomas/log4go" "github.com/mattermost/platform/model" ) +const ( + ENABLE_EXPERIMENTAL_REDIS = false +) + type LayeredStore struct { - TmpContext context.Context - ReactionStore ReactionStore - DatabaseLayer *SqlSupplier + TmpContext context.Context + ReactionStore ReactionStore + DatabaseLayer *SqlSupplier + LocalCacheLayer *LocalCacheSupplier + RedisLayer *RedisSupplier + LayerChainHead LayeredStoreSupplier } func NewLayeredStore() Store { - return &LayeredStore{ - TmpContext: context.TODO(), - ReactionStore: &LayeredReactionStore{}, - DatabaseLayer: NewSqlSupplier(), + store := &LayeredStore{ + TmpContext: context.TODO(), + DatabaseLayer: NewSqlSupplier(), + LocalCacheLayer: NewLocalCacheSupplier(), } + + store.ReactionStore = &LayeredReactionStore{store} + + // Setup the chain + if ENABLE_EXPERIMENTAL_REDIS { + l4g.Debug("Experimental redis enabled.") + store.RedisLayer = NewRedisSupplier() + store.RedisLayer.SetChainNext(store.DatabaseLayer) + store.LayerChainHead = store.RedisLayer + } else { + store.LocalCacheLayer.SetChainNext(store.DatabaseLayer) + store.LayerChainHead = store.LocalCacheLayer + } + + return store } -type QueryFunction func(LayeredStoreSupplier) LayeredStoreSupplierResult +type QueryFunction func(LayeredStoreSupplier) *LayeredStoreSupplierResult func (s *LayeredStore) RunQuery(queryFunction QueryFunction) StoreChannel { storeChannel := make(StoreChannel) go func() { - finalResult := StoreResult{} - // Logic for determining what layers to run - if result := queryFunction(s.DatabaseLayer); result.Err == nil { - finalResult.Data = result.Result - } else { - finalResult.Err = result.Err - } - - storeChannel <- finalResult + result := queryFunction(s.LayerChainHead) + storeChannel <- result.StoreResult }() return storeChannel @@ -116,7 +132,7 @@ func (s *LayeredStore) FileInfo() FileInfoStore { } func (s *LayeredStore) Reaction() ReactionStore { - return s.DatabaseLayer.Reaction() + return s.ReactionStore } func (s *LayeredStore) Job() JobStore { @@ -152,35 +168,25 @@ type LayeredReactionStore struct { } func (s *LayeredReactionStore) Save(reaction *model.Reaction) StoreChannel { - return s.RunQuery(func(supplier LayeredStoreSupplier) LayeredStoreSupplierResult { + return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { return supplier.ReactionSave(s.TmpContext, reaction) }) } func (s *LayeredReactionStore) Delete(reaction *model.Reaction) StoreChannel { - return s.RunQuery(func(supplier LayeredStoreSupplier) LayeredStoreSupplierResult { + return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { return supplier.ReactionDelete(s.TmpContext, reaction) }) } -// TODO: DELETE ME -func (s *LayeredReactionStore) InvalidateCacheForPost(postId string) { - return -} - -// TODO: DELETE ME -func (s *LayeredReactionStore) InvalidateCache() { - return -} - func (s *LayeredReactionStore) GetForPost(postId string, allowFromCache bool) StoreChannel { - return s.RunQuery(func(supplier LayeredStoreSupplier) LayeredStoreSupplierResult { + return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { return supplier.ReactionGetForPost(s.TmpContext, postId) }) } func (s *LayeredReactionStore) DeleteAllWithEmojiName(emojiName string) StoreChannel { - return s.RunQuery(func(supplier LayeredStoreSupplier) LayeredStoreSupplierResult { + return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult { return supplier.ReactionDeleteAllWithEmojiName(s.TmpContext, emojiName) }) } -- cgit v1.2.3-1-g7c22