github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/store/localcachelayer/terms_of_service_layer.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package localcachelayer
     5  
     6  import (
     7  	"github.com/vnforks/kid/v5/model"
     8  	"github.com/vnforks/kid/v5/store"
     9  )
    10  
    11  const (
    12  	LATEST_KEY = "latest"
    13  )
    14  
    15  type LocalCacheTermsOfServiceStore struct {
    16  	store.TermsOfServiceStore
    17  	rootStore *LocalCacheStore
    18  }
    19  
    20  func (s *LocalCacheTermsOfServiceStore) handleClusterInvalidateTermsOfService(msg *model.ClusterMessage) {
    21  	if msg.Data == CLEAR_CACHE_MESSAGE_DATA {
    22  		s.rootStore.termsOfServiceCache.Purge()
    23  	} else {
    24  		s.rootStore.termsOfServiceCache.Remove(msg.Data)
    25  	}
    26  }
    27  
    28  func (s LocalCacheTermsOfServiceStore) ClearCaches() {
    29  	s.rootStore.doClearCacheCluster(s.rootStore.termsOfServiceCache)
    30  
    31  	if s.rootStore.metrics != nil {
    32  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Terms Of Service - Purge")
    33  	}
    34  }
    35  
    36  func (s LocalCacheTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError) {
    37  	tos, err := s.TermsOfServiceStore.Save(termsOfService)
    38  
    39  	if err == nil {
    40  		s.rootStore.doStandardAddToCache(s.rootStore.termsOfServiceCache, tos.Id, tos)
    41  		s.rootStore.doInvalidateCacheCluster(s.rootStore.termsOfServiceCache, LATEST_KEY)
    42  	}
    43  	return tos, err
    44  }
    45  
    46  func (s LocalCacheTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) {
    47  	if allowFromCache {
    48  		if s.rootStore.termsOfServiceCache.Len() != 0 {
    49  			if cacheItem := s.rootStore.doStandardReadCache(s.rootStore.termsOfServiceCache, LATEST_KEY); cacheItem != nil {
    50  				return cacheItem.(*model.TermsOfService), nil
    51  			}
    52  		}
    53  	}
    54  
    55  	termsOfService, err := s.TermsOfServiceStore.GetLatest(allowFromCache)
    56  
    57  	if allowFromCache && err == nil {
    58  		s.rootStore.doStandardAddToCache(s.rootStore.termsOfServiceCache, termsOfService.Id, termsOfService)
    59  		s.rootStore.doStandardAddToCache(s.rootStore.termsOfServiceCache, LATEST_KEY, termsOfService)
    60  	}
    61  
    62  	return termsOfService, err
    63  }
    64  
    65  func (s LocalCacheTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) {
    66  	if allowFromCache {
    67  		if cacheItem := s.rootStore.doStandardReadCache(s.rootStore.termsOfServiceCache, id); cacheItem != nil {
    68  			return cacheItem.(*model.TermsOfService), nil
    69  		}
    70  	}
    71  
    72  	termsOfService, err := s.TermsOfServiceStore.Get(id, allowFromCache)
    73  
    74  	if allowFromCache && err == nil {
    75  		s.rootStore.doStandardAddToCache(s.rootStore.termsOfServiceCache, termsOfService.Id, termsOfService)
    76  	}
    77  
    78  	return termsOfService, err
    79  }