diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-01-27 14:07:34 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-27 14:07:34 -0500 |
commit | 097289f8e473c799ee752aa56e08f605110f5217 (patch) | |
tree | 424cd42d691b28d1c08852dc02a69d69f2b70a65 /store/sql_webhook_store.go | |
parent | 8eab04e944b3874f1fc4985344cbccec84c6002a (diff) | |
download | chat-097289f8e473c799ee752aa56e08f605110f5217.tar.gz chat-097289f8e473c799ee752aa56e08f605110f5217.tar.bz2 chat-097289f8e473c799ee752aa56e08f605110f5217.zip |
Merge 3.6.2 into master (#5211)
* Add webhook cache
* Add channel by name cache
* Fxing profiles in channels cache
* Fix merge
Diffstat (limited to 'store/sql_webhook_store.go')
-rw-r--r-- | store/sql_webhook_store.go | 40 |
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 |