github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/localcachelayer/webhook_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 type LocalCacheWebhookStore struct { 12 store.WebhookStore 13 rootStore *LocalCacheStore 14 } 15 16 func (s *LocalCacheWebhookStore) handleClusterInvalidateWebhook(msg *model.ClusterMessage) { 17 if msg.Data == ClearCacheMessageData { 18 s.rootStore.webhookCache.Purge() 19 } else { 20 s.rootStore.webhookCache.Remove(msg.Data) 21 } 22 } 23 24 func (s LocalCacheWebhookStore) ClearCaches() { 25 s.rootStore.doClearCacheCluster(s.rootStore.webhookCache) 26 27 if s.rootStore.metrics != nil { 28 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Webhook - Purge") 29 } 30 } 31 32 func (s LocalCacheWebhookStore) InvalidateWebhookCache(webhookId string) { 33 s.rootStore.doInvalidateCacheCluster(s.rootStore.webhookCache, webhookId) 34 if s.rootStore.metrics != nil { 35 s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Webhook - Remove by WebhookId") 36 } 37 } 38 39 func (s LocalCacheWebhookStore) GetIncoming(id string, allowFromCache bool) (*model.IncomingWebhook, error) { 40 if !allowFromCache { 41 return s.WebhookStore.GetIncoming(id, allowFromCache) 42 } 43 44 var incomingWebhook *model.IncomingWebhook 45 if err := s.rootStore.doStandardReadCache(s.rootStore.webhookCache, id, &incomingWebhook); err == nil { 46 return incomingWebhook, nil 47 } 48 49 incomingWebhook, err := s.WebhookStore.GetIncoming(id, allowFromCache) 50 if err != nil { 51 return nil, err 52 } 53 54 s.rootStore.doStandardAddToCache(s.rootStore.webhookCache, id, incomingWebhook) 55 56 return incomingWebhook, nil 57 } 58 59 func (s LocalCacheWebhookStore) DeleteIncoming(webhookId string, time int64) error { 60 err := s.WebhookStore.DeleteIncoming(webhookId, time) 61 if err != nil { 62 return err 63 } 64 65 s.InvalidateWebhookCache(webhookId) 66 return nil 67 } 68 69 func (s LocalCacheWebhookStore) PermanentDeleteIncomingByUser(userId string) error { 70 err := s.WebhookStore.PermanentDeleteIncomingByUser(userId) 71 if err != nil { 72 return err 73 } 74 75 s.ClearCaches() 76 return nil 77 } 78 79 func (s LocalCacheWebhookStore) PermanentDeleteIncomingByChannel(channelId string) error { 80 err := s.WebhookStore.PermanentDeleteIncomingByChannel(channelId) 81 if err != nil { 82 return err 83 } 84 85 s.ClearCaches() 86 return nil 87 }