github.com/mad-app/mattermost-server@v5.11.1+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  = 30 * 60
    17  
    18  	ROLE_CACHE_SIZE = 20000
    19  	ROLE_CACHE_SEC  = 30 * 60
    20  
    21  	SCHEME_CACHE_SIZE = 20000
    22  	SCHEME_CACHE_SEC  = 30 * 60
    23  
    24  	GROUP_CACHE_SIZE = 20000
    25  	GROUP_CACHE_SEC  = 30 * 60
    26  
    27  	CLEAR_CACHE_MESSAGE_DATA = ""
    28  )
    29  
    30  type LocalCacheSupplier struct {
    31  	next          LayeredStoreSupplier
    32  	reactionCache *utils.Cache
    33  	roleCache     *utils.Cache
    34  	schemeCache   *utils.Cache
    35  	metrics       einterfaces.MetricsInterface
    36  	cluster       einterfaces.ClusterInterface
    37  	groupCache    *utils.Cache
    38  }
    39  
    40  // Caching Interface
    41  type ObjectCache interface {
    42  	AddWithExpiresInSecs(key, value interface{}, expireAtSecs int64)
    43  	AddWithDefaultExpires(key, value interface{})
    44  	Purge()
    45  	Get(key interface{}) (value interface{}, ok bool)
    46  	Remove(key interface{})
    47  	Len() int
    48  	Name() string
    49  	GetInvalidateClusterEvent() string
    50  }
    51  
    52  func NewLocalCacheSupplier(metrics einterfaces.MetricsInterface, cluster einterfaces.ClusterInterface) *LocalCacheSupplier {
    53  	supplier := &LocalCacheSupplier{
    54  		reactionCache: utils.NewLruWithParams(REACTION_CACHE_SIZE, "Reaction", REACTION_CACHE_SEC, model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_REACTIONS),
    55  		roleCache:     utils.NewLruWithParams(ROLE_CACHE_SIZE, "Role", ROLE_CACHE_SEC, model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_ROLES),
    56  		schemeCache:   utils.NewLruWithParams(SCHEME_CACHE_SIZE, "Scheme", SCHEME_CACHE_SEC, model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_SCHEMES),
    57  		groupCache:    utils.NewLruWithParams(GROUP_CACHE_SIZE, "Group", GROUP_CACHE_SEC, model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_GROUPS),
    58  		metrics:       metrics,
    59  		cluster:       cluster,
    60  	}
    61  
    62  	if cluster != nil {
    63  		cluster.RegisterClusterMessageHandler(model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_REACTIONS, supplier.handleClusterInvalidateReaction)
    64  		cluster.RegisterClusterMessageHandler(model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_ROLES, supplier.handleClusterInvalidateRole)
    65  		cluster.RegisterClusterMessageHandler(model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_GROUPS, supplier.handleClusterInvalidateGroup)
    66  	}
    67  
    68  	return supplier
    69  }
    70  
    71  func (s *LocalCacheSupplier) SetChainNext(next LayeredStoreSupplier) {
    72  	s.next = next
    73  }
    74  
    75  func (s *LocalCacheSupplier) Next() LayeredStoreSupplier {
    76  	return s.next
    77  }
    78  
    79  func (s *LocalCacheSupplier) doStandardReadCache(ctx context.Context, cache ObjectCache, key string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    80  	if hintsContains(hints, LSH_NO_CACHE) {
    81  		if s.metrics != nil {
    82  			s.metrics.IncrementMemCacheMissCounter(cache.Name())
    83  		}
    84  		return nil
    85  	}
    86  
    87  	if cacheItem, ok := cache.Get(key); ok {
    88  		if s.metrics != nil {
    89  			s.metrics.IncrementMemCacheHitCounter(cache.Name())
    90  		}
    91  		result := NewSupplierResult()
    92  		result.Data = cacheItem
    93  		return result
    94  	}
    95  
    96  	if s.metrics != nil {
    97  		s.metrics.IncrementMemCacheMissCounter(cache.Name())
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  func (s *LocalCacheSupplier) doStandardAddToCache(ctx context.Context, cache ObjectCache, key string, result *LayeredStoreSupplierResult, hints ...LayeredStoreHint) {
   104  	if result.Err == nil && result.Data != nil {
   105  		cache.AddWithDefaultExpires(key, result.Data)
   106  	}
   107  }
   108  
   109  func (s *LocalCacheSupplier) doInvalidateCacheCluster(cache ObjectCache, key string) {
   110  	cache.Remove(key)
   111  	if s.cluster != nil {
   112  		msg := &model.ClusterMessage{
   113  			Event:    cache.GetInvalidateClusterEvent(),
   114  			SendType: model.CLUSTER_SEND_BEST_EFFORT,
   115  			Data:     key,
   116  		}
   117  		s.cluster.SendClusterMessage(msg)
   118  	}
   119  }
   120  
   121  func (s *LocalCacheSupplier) doClearCacheCluster(cache ObjectCache) {
   122  	cache.Purge()
   123  	if s.cluster != nil {
   124  		msg := &model.ClusterMessage{
   125  			Event:    cache.GetInvalidateClusterEvent(),
   126  			SendType: model.CLUSTER_SEND_BEST_EFFORT,
   127  			Data:     CLEAR_CACHE_MESSAGE_DATA,
   128  		}
   129  		s.cluster.SendClusterMessage(msg)
   130  	}
   131  }
   132  
   133  func (s *LocalCacheSupplier) Invalidate() {
   134  	s.doClearCacheCluster(s.reactionCache)
   135  	s.doClearCacheCluster(s.roleCache)
   136  	s.doClearCacheCluster(s.schemeCache)
   137  }