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