github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/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/mattermost/mattermost-server/v5/model" 8 "github.com/mattermost/mattermost-server/v5/store" 9 ) 10 11 const ( 12 LatestKey = "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 == ClearCacheMessageData { 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, error) { 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, LatestKey) 42 } 43 return tos, err 44 } 45 46 func (s LocalCacheTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, error) { 47 if allowFromCache { 48 if len, err := s.rootStore.termsOfServiceCache.Len(); err == nil && len != 0 { 49 var cacheItem *model.TermsOfService 50 if err := s.rootStore.doStandardReadCache(s.rootStore.termsOfServiceCache, LatestKey, &cacheItem); err == nil { 51 return cacheItem, nil 52 } 53 } 54 } 55 56 termsOfService, err := s.TermsOfServiceStore.GetLatest(allowFromCache) 57 58 if allowFromCache && err == nil { 59 s.rootStore.doStandardAddToCache(s.rootStore.termsOfServiceCache, termsOfService.Id, termsOfService) 60 s.rootStore.doStandardAddToCache(s.rootStore.termsOfServiceCache, LatestKey, termsOfService) 61 } 62 63 return termsOfService, err 64 } 65 66 func (s LocalCacheTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, error) { 67 if allowFromCache { 68 var cacheItem *model.TermsOfService 69 if err := s.rootStore.doStandardReadCache(s.rootStore.termsOfServiceCache, id, &cacheItem); err == nil { 70 return cacheItem, nil 71 } 72 } 73 74 termsOfService, err := s.TermsOfServiceStore.Get(id, allowFromCache) 75 76 if allowFromCache && err == nil { 77 s.rootStore.doStandardAddToCache(s.rootStore.termsOfServiceCache, termsOfService.Id, termsOfService) 78 } 79 80 return termsOfService, err 81 }