github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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/vnforks/kid/v5/model" 8 "github.com/vnforks/kid/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 == CLEAR_CACHE_MESSAGE_DATA { 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, *model.AppError) { 40 if !allowFromCache { 41 return s.WebhookStore.GetIncoming(id, allowFromCache) 42 } 43 44 if incomingWebhook := s.rootStore.doStandardReadCache(s.rootStore.webhookCache, id); incomingWebhook != nil { 45 return incomingWebhook.(*model.IncomingWebhook), nil 46 } 47 48 incomingWebhook, err := s.WebhookStore.GetIncoming(id, allowFromCache) 49 if err != nil { 50 return nil, err 51 } 52 53 s.rootStore.doStandardAddToCache(s.rootStore.webhookCache, id, incomingWebhook) 54 55 return incomingWebhook, nil 56 } 57 58 func (s LocalCacheWebhookStore) DeleteIncoming(webhookId string, time int64) *model.AppError { 59 err := s.WebhookStore.DeleteIncoming(webhookId, time) 60 if err != nil { 61 return err 62 } 63 64 s.InvalidateWebhookCache(webhookId) 65 return nil 66 } 67 68 func (s LocalCacheWebhookStore) PermanentDeleteIncomingByUser(userId string) *model.AppError { 69 err := s.WebhookStore.PermanentDeleteIncomingByUser(userId) 70 if err != nil { 71 return err 72 } 73 74 s.ClearCaches() 75 return nil 76 } 77 78 func (s LocalCacheWebhookStore) PermanentDeleteIncomingByClass(classId string) *model.AppError { 79 err := s.WebhookStore.PermanentDeleteIncomingByClass(classId) 80 if err != nil { 81 return err 82 } 83 84 s.ClearCaches() 85 return nil 86 }