summaryrefslogtreecommitdiffstats
path: root/store/sql_webhook_store.go
diff options
context:
space:
mode:
Diffstat (limited to 'store/sql_webhook_store.go')
-rw-r--r--store/sql_webhook_store.go40
1 files changed, 39 insertions, 1 deletions
diff --git a/store/sql_webhook_store.go b/store/sql_webhook_store.go
index 1e1740796..4022aff7f 100644
--- a/store/sql_webhook_store.go
+++ b/store/sql_webhook_store.go
@@ -4,13 +4,26 @@
package store
import (
+ "github.com/mattermost/platform/einterfaces"
"github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
)
type SqlWebhookStore struct {
*SqlStore
}
+const (
+ WEBHOOK_CACHE_SIZE = 25000
+ WEBHOOK_CACHE_SEC = 900 // 15 minutes
+)
+
+var webhookCache = utils.NewLru(WEBHOOK_CACHE_SIZE)
+
+func ClearWebhookCaches() {
+ webhookCache.Purge()
+}
+
func NewSqlWebhookStore(sqlStore *SqlStore) WebhookStore {
s := &SqlWebhookStore{sqlStore}
@@ -54,6 +67,10 @@ func (s SqlWebhookStore) CreateIndexesIfNotExists() {
s.CreateIndexIfNotExists("idx_outgoing_webhook_delete_at", "OutgoingWebhooks", "DeleteAt")
}
+func (s SqlWebhookStore) InvalidateWebhookCache(webhookId string) {
+ webhookCache.Remove(webhookId)
+}
+
func (s SqlWebhookStore) SaveIncoming(webhook *model.IncomingWebhook) StoreChannel {
storeChannel := make(StoreChannel, 1)
@@ -88,18 +105,39 @@ func (s SqlWebhookStore) SaveIncoming(webhook *model.IncomingWebhook) StoreChann
return storeChannel
}
-func (s SqlWebhookStore) GetIncoming(id string) StoreChannel {
+func (s SqlWebhookStore) GetIncoming(id string, allowFromCache bool) StoreChannel {
storeChannel := make(StoreChannel, 1)
go func() {
result := StoreResult{}
+ if allowFromCache {
+ metrics := einterfaces.GetMetricsInterface()
+ if cacheItem, ok := webhookCache.Get(id); ok {
+ if metrics != nil {
+ metrics.IncrementMemCacheHitCounter("Webhook")
+ }
+ result.Data = cacheItem.(*model.IncomingWebhook)
+ storeChannel <- result
+ close(storeChannel)
+ return
+ } else {
+ if metrics != nil {
+ metrics.IncrementMemCacheMissCounter("Webhook")
+ }
+ }
+ }
+
var webhook model.IncomingWebhook
if err := s.GetReplica().SelectOne(&webhook, "SELECT * FROM IncomingWebhooks WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id}); err != nil {
result.Err = model.NewLocAppError("SqlWebhookStore.GetIncoming", "store.sql_webhooks.get_incoming.app_error", nil, "id="+id+", err="+err.Error())
}
+ if result.Err == nil {
+ webhookCache.AddWithExpiresInSecs(id, &webhook, WEBHOOK_CACHE_SEC)
+ }
+
result.Data = &webhook
storeChannel <- result