summaryrefslogtreecommitdiffstats
path: root/store/local_cache_supplier.go
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2017-07-31 08:15:23 -0700
committerGitHub <noreply@github.com>2017-07-31 08:15:23 -0700
commit09b49c26ddfdb20ced61e7dfd4192e750ce40449 (patch)
tree1288d069cc8a199b8eb3b858935dffd377ee3d2d /store/local_cache_supplier.go
parent6f4e38d129ffaf469d40fc8596d3957ee94d21e9 (diff)
downloadchat-09b49c26ddfdb20ced61e7dfd4192e750ce40449.tar.gz
chat-09b49c26ddfdb20ced61e7dfd4192e750ce40449.tar.bz2
chat-09b49c26ddfdb20ced61e7dfd4192e750ce40449.zip
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
Diffstat (limited to 'store/local_cache_supplier.go')
-rw-r--r--store/local_cache_supplier.go104
1 files changed, 104 insertions, 0 deletions
diff --git a/store/local_cache_supplier.go b/store/local_cache_supplier.go
new file mode 100644
index 000000000..63c050485
--- /dev/null
+++ b/store/local_cache_supplier.go
@@ -0,0 +1,104 @@
+// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package store
+
+import (
+ "context"
+
+ "github.com/mattermost/platform/einterfaces"
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
+)
+
+const (
+ REACTION_CACHE_SIZE = 20000
+ REACTION_CACHE_SEC = 1800 // 30 minutes
+
+ CLEAR_CACHE_MESSAGE_DATA = ""
+)
+
+type LocalCacheSupplier struct {
+ next LayeredStoreSupplier
+ reactionCache *utils.Cache
+}
+
+func NewLocalCacheSupplier() *LocalCacheSupplier {
+ supplier := &LocalCacheSupplier{
+ reactionCache: utils.NewLruWithParams(REACTION_CACHE_SIZE, "Reaction", REACTION_CACHE_SEC, model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_REACTIONS),
+ }
+
+ registerClusterHandlers(supplier)
+
+ return supplier
+}
+
+func registerClusterHandlers(supplier *LocalCacheSupplier) {
+ if cluster := einterfaces.GetClusterInterface(); cluster != nil {
+ cluster.RegisterClusterMessageHandler(model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_REACTIONS, supplier.handleClusterInvalidateReaction)
+ }
+}
+
+func (s *LocalCacheSupplier) SetChainNext(next LayeredStoreSupplier) {
+ s.next = next
+}
+
+func (s *LocalCacheSupplier) Next() LayeredStoreSupplier {
+ return s.next
+}
+
+func doStandardReadCache(ctx context.Context, cache utils.ObjectCache, key string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
+ metrics := einterfaces.GetMetricsInterface()
+
+ if hintsContains(hints, LSH_NO_CACHE) {
+ if metrics != nil {
+ metrics.IncrementMemCacheMissCounter(cache.Name())
+ }
+ return nil
+ }
+
+ if cacheItem, ok := cache.Get(key); ok {
+ if metrics != nil {
+ metrics.IncrementMemCacheHitCounter(cache.Name())
+ }
+ result := NewSupplierResult()
+ result.Data = cacheItem
+ return result
+ }
+
+ if metrics != nil {
+ metrics.IncrementMemCacheMissCounter(cache.Name())
+ }
+
+ return nil
+}
+
+func doStandardAddToCache(ctx context.Context, cache utils.ObjectCache, key string, result *LayeredStoreSupplierResult, hints ...LayeredStoreHint) {
+ if result.Err == nil && result.Data != nil {
+ cache.AddWithDefaultExpires(key, result.Data)
+ }
+}
+
+func doInvalidateCacheCluster(cache utils.ObjectCache, key string) {
+ cache.Remove(key)
+ if einterfaces.GetClusterInterface() != nil {
+ msg := &model.ClusterMessage{
+ Event: cache.GetInvalidateClusterEvent(),
+ SendType: model.CLUSTER_SEND_BEST_EFFORT,
+ Data: key,
+ }
+ einterfaces.GetClusterInterface().SendClusterMessage(msg)
+ }
+}
+
+func doClearCacheCluster(cache utils.ObjectCache) {
+ cache.Purge()
+ if einterfaces.GetClusterInterface() != nil {
+ msg := &model.ClusterMessage{
+ Event: cache.GetInvalidateClusterEvent(),
+ SendType: model.CLUSTER_SEND_BEST_EFFORT,
+ Data: CLEAR_CACHE_MESSAGE_DATA,
+ }
+ einterfaces.GetClusterInterface().SendClusterMessage(msg)
+ }
+}