github.com/dschalla/mattermost-server@v4.8.1-rc1+incompatible/store/local_cache_supplier.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package store
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/mattermost/mattermost-server/einterfaces"
    10  	"github.com/mattermost/mattermost-server/model"
    11  	"github.com/mattermost/mattermost-server/utils"
    12  )
    13  
    14  const (
    15  	REACTION_CACHE_SIZE = 20000
    16  	REACTION_CACHE_SEC  = 1800 // 30 minutes
    17  
    18  	CLEAR_CACHE_MESSAGE_DATA = ""
    19  )
    20  
    21  type LocalCacheSupplier struct {
    22  	next          LayeredStoreSupplier
    23  	reactionCache *utils.Cache
    24  	metrics       einterfaces.MetricsInterface
    25  	cluster       einterfaces.ClusterInterface
    26  }
    27  
    28  func NewLocalCacheSupplier(metrics einterfaces.MetricsInterface, cluster einterfaces.ClusterInterface) *LocalCacheSupplier {
    29  	supplier := &LocalCacheSupplier{
    30  		reactionCache: utils.NewLruWithParams(REACTION_CACHE_SIZE, "Reaction", REACTION_CACHE_SEC, model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_REACTIONS),
    31  		metrics:       metrics,
    32  		cluster:       cluster,
    33  	}
    34  
    35  	if cluster != nil {
    36  		cluster.RegisterClusterMessageHandler(model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_REACTIONS, supplier.handleClusterInvalidateReaction)
    37  	}
    38  
    39  	return supplier
    40  }
    41  
    42  func (s *LocalCacheSupplier) SetChainNext(next LayeredStoreSupplier) {
    43  	s.next = next
    44  }
    45  
    46  func (s *LocalCacheSupplier) Next() LayeredStoreSupplier {
    47  	return s.next
    48  }
    49  
    50  func (s *LocalCacheSupplier) doStandardReadCache(ctx context.Context, cache utils.ObjectCache, key string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    51  	if hintsContains(hints, LSH_NO_CACHE) {
    52  		if s.metrics != nil {
    53  			s.metrics.IncrementMemCacheMissCounter(cache.Name())
    54  		}
    55  		return nil
    56  	}
    57  
    58  	if cacheItem, ok := cache.Get(key); ok {
    59  		if s.metrics != nil {
    60  			s.metrics.IncrementMemCacheHitCounter(cache.Name())
    61  		}
    62  		result := NewSupplierResult()
    63  		result.Data = cacheItem
    64  		return result
    65  	}
    66  
    67  	if s.metrics != nil {
    68  		s.metrics.IncrementMemCacheMissCounter(cache.Name())
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func (s *LocalCacheSupplier) doStandardAddToCache(ctx context.Context, cache utils.ObjectCache, key string, result *LayeredStoreSupplierResult, hints ...LayeredStoreHint) {
    75  	if result.Err == nil && result.Data != nil {
    76  		cache.AddWithDefaultExpires(key, result.Data)
    77  	}
    78  }
    79  
    80  func (s *LocalCacheSupplier) doInvalidateCacheCluster(cache utils.ObjectCache, key string) {
    81  	cache.Remove(key)
    82  	if s.cluster != nil {
    83  		msg := &model.ClusterMessage{
    84  			Event:    cache.GetInvalidateClusterEvent(),
    85  			SendType: model.CLUSTER_SEND_BEST_EFFORT,
    86  			Data:     key,
    87  		}
    88  		s.cluster.SendClusterMessage(msg)
    89  	}
    90  }
    91  
    92  func (s *LocalCacheSupplier) doClearCacheCluster(cache utils.ObjectCache) {
    93  	cache.Purge()
    94  	if s.cluster != nil {
    95  		msg := &model.ClusterMessage{
    96  			Event:    cache.GetInvalidateClusterEvent(),
    97  			SendType: model.CLUSTER_SEND_BEST_EFFORT,
    98  			Data:     CLEAR_CACHE_MESSAGE_DATA,
    99  		}
   100  		s.cluster.SendClusterMessage(msg)
   101  	}
   102  }