github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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  	CLEAR_CACHE_MESSAGE_DATA = ""
    25  )
    26  
    27  type LocalCacheSupplier struct {
    28  	next          LayeredStoreSupplier
    29  	reactionCache *utils.Cache
    30  	roleCache     *utils.Cache
    31  	schemeCache   *utils.Cache
    32  	metrics       einterfaces.MetricsInterface
    33  	cluster       einterfaces.ClusterInterface
    34  }
    35  
    36  func NewLocalCacheSupplier(metrics einterfaces.MetricsInterface, cluster einterfaces.ClusterInterface) *LocalCacheSupplier {
    37  	supplier := &LocalCacheSupplier{
    38  		reactionCache: utils.NewLruWithParams(REACTION_CACHE_SIZE, "Reaction", REACTION_CACHE_SEC, model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_REACTIONS),
    39  		roleCache:     utils.NewLruWithParams(ROLE_CACHE_SIZE, "Role", ROLE_CACHE_SEC, model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_ROLES),
    40  		schemeCache:   utils.NewLruWithParams(SCHEME_CACHE_SIZE, "Scheme", SCHEME_CACHE_SEC, model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_SCHEMES),
    41  		metrics:       metrics,
    42  		cluster:       cluster,
    43  	}
    44  
    45  	if cluster != nil {
    46  		cluster.RegisterClusterMessageHandler(model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_REACTIONS, supplier.handleClusterInvalidateReaction)
    47  		cluster.RegisterClusterMessageHandler(model.CLUSTER_EVENT_INVALIDATE_CACHE_FOR_ROLES, supplier.handleClusterInvalidateRole)
    48  	}
    49  
    50  	return supplier
    51  }
    52  
    53  func (s *LocalCacheSupplier) SetChainNext(next LayeredStoreSupplier) {
    54  	s.next = next
    55  }
    56  
    57  func (s *LocalCacheSupplier) Next() LayeredStoreSupplier {
    58  	return s.next
    59  }
    60  
    61  func (s *LocalCacheSupplier) doStandardReadCache(ctx context.Context, cache utils.ObjectCache, key string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
    62  	if hintsContains(hints, LSH_NO_CACHE) {
    63  		if s.metrics != nil {
    64  			s.metrics.IncrementMemCacheMissCounter(cache.Name())
    65  		}
    66  		return nil
    67  	}
    68  
    69  	if cacheItem, ok := cache.Get(key); ok {
    70  		if s.metrics != nil {
    71  			s.metrics.IncrementMemCacheHitCounter(cache.Name())
    72  		}
    73  		result := NewSupplierResult()
    74  		result.Data = cacheItem
    75  		return result
    76  	}
    77  
    78  	if s.metrics != nil {
    79  		s.metrics.IncrementMemCacheMissCounter(cache.Name())
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  func (s *LocalCacheSupplier) doStandardAddToCache(ctx context.Context, cache utils.ObjectCache, key string, result *LayeredStoreSupplierResult, hints ...LayeredStoreHint) {
    86  	if result.Err == nil && result.Data != nil {
    87  		cache.AddWithDefaultExpires(key, result.Data)
    88  	}
    89  }
    90  
    91  func (s *LocalCacheSupplier) doInvalidateCacheCluster(cache utils.ObjectCache, key string) {
    92  	cache.Remove(key)
    93  	if s.cluster != nil {
    94  		msg := &model.ClusterMessage{
    95  			Event:    cache.GetInvalidateClusterEvent(),
    96  			SendType: model.CLUSTER_SEND_BEST_EFFORT,
    97  			Data:     key,
    98  		}
    99  		s.cluster.SendClusterMessage(msg)
   100  	}
   101  }
   102  
   103  func (s *LocalCacheSupplier) doClearCacheCluster(cache utils.ObjectCache) {
   104  	cache.Purge()
   105  	if s.cluster != nil {
   106  		msg := &model.ClusterMessage{
   107  			Event:    cache.GetInvalidateClusterEvent(),
   108  			SendType: model.CLUSTER_SEND_BEST_EFFORT,
   109  			Data:     CLEAR_CACHE_MESSAGE_DATA,
   110  		}
   111  		s.cluster.SendClusterMessage(msg)
   112  	}
   113  }