From 097289f8e473c799ee752aa56e08f605110f5217 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Fri, 27 Jan 2017 14:07:34 -0500 Subject: Merge 3.6.2 into master (#5211) * Add webhook cache * Add channel by name cache * Fxing profiles in channels cache * Fix merge --- api/command_join.go | 2 +- api/command_msg.go | 2 +- api/webhook.go | 8 +++++--- app/admin.go | 1 + app/channel.go | 19 ++++++++++--------- app/import.go | 2 +- app/notification.go | 6 ------ app/post.go | 6 +++--- app/slackimport.go | 2 +- app/team.go | 2 +- app/web_hub.go | 37 +++++++++++++++++++++++++++++++++---- cmd/platform/channelargs.go | 2 +- cmd/platform/oldcommands.go | 4 ++-- einterfaces/cluster.go | 3 +++ store/sql_channel_store.go | 34 +++++++++++++++++++++++++++++----- store/sql_channel_store_test.go | 18 +++++++++++++++--- store/sql_webhook_store.go | 40 +++++++++++++++++++++++++++++++++++++++- store/sql_webhook_store_test.go | 28 ++++++++++++++++++++++------ store/store.go | 8 +++++--- 19 files changed, 173 insertions(+), 51 deletions(-) diff --git a/api/command_join.go b/api/command_join.go index bad176656..17deb02b7 100644 --- a/api/command_join.go +++ b/api/command_join.go @@ -34,7 +34,7 @@ func (me *JoinProvider) GetCommand(c *Context) *model.Command { } func (me *JoinProvider) DoCommand(c *Context, args *model.CommandArgs, message string) *model.CommandResponse { - if result := <-app.Srv.Store.Channel().GetByName(c.TeamId, message); result.Err != nil { + if result := <-app.Srv.Store.Channel().GetByName(c.TeamId, message, true); result.Err != nil { return &model.CommandResponse{Text: c.T("api.command_join.list.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } else { channel := result.Data.(*model.Channel) diff --git a/api/command_msg.go b/api/command_msg.go index 86203c2cd..f7f31ed9a 100644 --- a/api/command_msg.go +++ b/api/command_msg.go @@ -64,7 +64,7 @@ func (me *msgProvider) DoCommand(c *Context, args *model.CommandArgs, message st channelName := model.GetDMNameFromIds(c.Session.UserId, userProfile.Id) targetChannelId := "" - if channel := <-app.Srv.Store.Channel().GetByName(c.TeamId, channelName); channel.Err != nil { + if channel := <-app.Srv.Store.Channel().GetByName(c.TeamId, channelName, true); channel.Err != nil { if channel.Err.Id == "store.sql_channel.get_by_name.missing.app_error" { if directChannel, err := app.CreateDirectChannel(c.Session.UserId, userProfile.Id); err != nil { c.Err = err diff --git a/api/webhook.go b/api/webhook.go index 5d36409eb..248df6726 100644 --- a/api/webhook.go +++ b/api/webhook.go @@ -109,7 +109,7 @@ func deleteIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) { return } - if result := <-app.Srv.Store.Webhook().GetIncoming(id); result.Err != nil { + if result := <-app.Srv.Store.Webhook().GetIncoming(id, true); result.Err != nil { c.Err = result.Err return } else { @@ -125,6 +125,8 @@ func deleteIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) { return } + app.InvalidateCacheForWebhook(id) + c.LogAudit("success") w.Write([]byte(model.MapToJson(props))) } @@ -352,7 +354,7 @@ func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) id := params["id"] - hchan := app.Srv.Store.Webhook().GetIncoming(id) + hchan := app.Srv.Store.Webhook().GetIncoming(id, true) r.ParseForm() @@ -448,7 +450,7 @@ func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) { channelName = channelName[1:] } - cchan = app.Srv.Store.Channel().GetByName(hook.TeamId, channelName) + cchan = app.Srv.Store.Channel().GetByName(hook.TeamId, channelName, true) } else { cchan = app.Srv.Store.Channel().Get(hook.ChannelId, true) } diff --git a/app/admin.go b/app/admin.go index 51e69da57..00d60a802 100644 --- a/app/admin.go +++ b/app/admin.go @@ -89,6 +89,7 @@ func InvalidateAllCachesSkipSend() { store.ClearChannelCaches() store.ClearUserCaches() store.ClearPostCaches() + store.ClearWebhookCaches() } func GetConfig() *model.Config { diff --git a/app/channel.go b/app/channel.go index 1844e3177..c9199f829 100644 --- a/app/channel.go +++ b/app/channel.go @@ -88,7 +88,7 @@ func CreateDefaultChannels(teamId string) ([]*model.Channel, *model.AppError) { func JoinDefaultChannels(teamId string, user *model.User, channelRole string) *model.AppError { var err *model.AppError = nil - if result := <-Srv.Store.Channel().GetByName(teamId, "town-square"); result.Err != nil { + if result := <-Srv.Store.Channel().GetByName(teamId, "town-square", true); result.Err != nil { err = result.Err } else { cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, @@ -105,14 +105,14 @@ func JoinDefaultChannels(teamId string, user *model.User, channelRole string) *m UserId: user.Id, } - InvalidateCacheForChannel(result.Data.(*model.Channel).Id) + InvalidateCacheForChannelMembers(result.Data.(*model.Channel).Id) if _, err := CreatePost(post, teamId, false); err != nil { l4g.Error(utils.T("api.channel.post_user_add_remove_message_and_forget.error"), err) } } - if result := <-Srv.Store.Channel().GetByName(teamId, "off-topic"); result.Err != nil { + if result := <-Srv.Store.Channel().GetByName(teamId, "off-topic", true); result.Err != nil { err = result.Err } else { cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, @@ -129,7 +129,7 @@ func JoinDefaultChannels(teamId string, user *model.User, channelRole string) *m UserId: user.Id, } - InvalidateCacheForChannel(result.Data.(*model.Channel).Id) + InvalidateCacheForChannelMembers(result.Data.(*model.Channel).Id) if _, err := CreatePost(post, teamId, false); err != nil { l4g.Error(utils.T("api.channel.post_user_add_remove_message_and_forget.error"), err) @@ -195,7 +195,7 @@ func UpdateChannel(channel *model.Channel) (*model.Channel, *model.AppError) { if result := <-Srv.Store.Channel().Update(channel); result.Err != nil { return nil, result.Err } else { - InvalidateCacheForChannel(channel.Id) + InvalidateCacheForChannel(channel) return channel, nil } } @@ -287,6 +287,7 @@ func DeleteChannel(channel *model.Channel, userId string) *model.AppError { if result := <-Srv.Store.Webhook().DeleteIncoming(hook.Id, now); result.Err != nil { l4g.Error(utils.T("api.channel.delete_channel.incoming_webhook.error"), hook.Id) } + InvalidateCacheForWebhook(hook.Id) } for _, hook := range outgoingHooks { @@ -298,7 +299,7 @@ func DeleteChannel(channel *model.Channel, userId string) *model.AppError { if dresult := <-Srv.Store.Channel().Delete(channel.Id, model.GetMillis()); dresult.Err != nil { return dresult.Err } - InvalidateCacheForChannel(channel.Id) + InvalidateCacheForChannel(channel) message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_DELETED, channel.TeamId, "", "", nil) message.Add("channel_id", channel.Id) @@ -351,7 +352,7 @@ func AddUserToChannel(user *model.User, channel *model.Channel) (*model.ChannelM } InvalidateCacheForUser(user.Id) - InvalidateCacheForChannel(channel.Id) + InvalidateCacheForChannelMembers(channel.Id) message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_USER_ADDED, "", channel.Id, "", nil) message.Add("user_id", user.Id) @@ -508,7 +509,7 @@ func GetChannel(channelId string) (*model.Channel, *model.AppError) { } func GetChannelByName(channelName, teamId string) (*model.Channel, *model.AppError) { - if result := <-Srv.Store.Channel().GetByName(teamId, channelName); result.Err != nil { + if result := <-Srv.Store.Channel().GetByName(teamId, channelName, true); result.Err != nil { return nil, result.Err } else { return result.Data.(*model.Channel), nil @@ -650,7 +651,7 @@ func RemoveUserFromChannel(userIdToRemove string, removerUserId string, channel } InvalidateCacheForUser(userIdToRemove) - InvalidateCacheForChannel(channel.Id) + InvalidateCacheForChannelMembers(channel.Id) message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_USER_REMOVED, "", channel.Id, "", nil) message.Add("user_id", userIdToRemove) diff --git a/app/import.go b/app/import.go index 882641799..26981f0c2 100644 --- a/app/import.go +++ b/app/import.go @@ -182,7 +182,7 @@ func ImportChannel(data *ChannelImportData, dryRun bool) *model.AppError { } var channel *model.Channel - if result := <-Srv.Store.Channel().GetByNameIncludeDeleted(team.Id, *data.Name); result.Err == nil { + if result := <-Srv.Store.Channel().GetByNameIncludeDeleted(team.Id, *data.Name, true); result.Err == nil { channel = result.Data.(*model.Channel) } else { channel = &model.Channel{} diff --git a/app/notification.go b/app/notification.go index c65635f60..9ad0b346b 100644 --- a/app/notification.go +++ b/app/notification.go @@ -35,12 +35,6 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe profileMap = result.Data.(map[string]*model.User) } - // If the user who made the post isn't in the channel, don't send a notification - if _, ok := profileMap[post.UserId]; !ok && post.Props["from_webhook"] != "true" { - l4g.Debug(utils.T("api.post.send_notifications.user_id.debug"), post.Id, channel.Id, post.UserId) - return []string{}, nil - } - mentionedUserIds := make(map[string]bool) allActivityPushUserIds := []string{} hereNotification := false diff --git a/app/post.go b/app/post.go index 6d34cc035..d7bc2cf71 100644 --- a/app/post.go +++ b/app/post.go @@ -110,9 +110,6 @@ func CreatePost(post *model.Post, teamId string, triggerWebhooks bool) (*model.P } } - InvalidateCacheForChannel(rpost.ChannelId) - InvalidateCacheForChannelPosts(rpost.ChannelId) - if err := handlePostEvents(rpost, teamId, triggerWebhooks); err != nil { return nil, err } @@ -139,6 +136,9 @@ func handlePostEvents(post *model.Post, teamId string, triggerWebhooks bool) *mo channel = result.Data.(*model.Channel) } + InvalidateCacheForChannel(channel) + InvalidateCacheForChannelPosts(channel.Id) + var user *model.User if result := <-uchan; result.Err != nil { return result.Err diff --git a/app/slackimport.go b/app/slackimport.go index edeb601e2..3289f140e 100644 --- a/app/slackimport.go +++ b/app/slackimport.go @@ -472,7 +472,7 @@ func SlackAddChannels(teamId string, slackchannels []SlackChannel, posts map[str newChannel = SlackSanitiseChannelProperties(newChannel) var mChannel *model.Channel - if result := <-Srv.Store.Channel().GetByName(teamId, sChannel.Name); result.Err == nil { + if result := <-Srv.Store.Channel().GetByName(teamId, sChannel.Name, true); result.Err == nil { // The channel already exists as an active channel. Merge with the existing one. mChannel = result.Data.(*model.Channel) log.WriteString(utils.T("api.slackimport.slack_add_channels.merge", map[string]interface{}{"DisplayName": newChannel.DisplayName})) diff --git a/app/team.go b/app/team.go index aabdc0bfd..3c7145818 100644 --- a/app/team.go +++ b/app/team.go @@ -408,7 +408,7 @@ func LeaveTeam(team *model.Team, user *model.User) *model.AppError { for _, channel := range *channelList { if channel.Type != model.CHANNEL_DIRECT { - InvalidateCacheForChannel(channel.Id) + InvalidateCacheForChannelMembers(channel.Id) if result := <-Srv.Store.Channel().RemoveMember(channel.Id, user.Id); result.Err != nil { return result.Err } diff --git a/app/web_hub.go b/app/web_hub.go index 28d2c0095..c8fbfbc34 100644 --- a/app/web_hub.go +++ b/app/web_hub.go @@ -103,18 +103,35 @@ func PublishSkipClusterSend(message *model.WebSocketEvent) { } } -func InvalidateCacheForChannel(channelId string) { - InvalidateCacheForChannelSkipClusterSend(channelId) +func InvalidateCacheForChannel(channel *model.Channel) { + InvalidateCacheForChannelSkipClusterSend(channel.Id) + InvalidateCacheForChannelByNameSkipClusterSend(channel.TeamId, channel.Name) if cluster := einterfaces.GetClusterInterface(); cluster != nil { - cluster.InvalidateCacheForChannel(channelId) + cluster.InvalidateCacheForChannel(channel.Id) + cluster.InvalidateCacheForChannelByName(channel.TeamId, channel.Name) + } +} + +func InvalidateCacheForChannelMembers(channelId string) { + InvalidateCacheForChannelMembersSkipClusterSend(channelId) + + if cluster := einterfaces.GetClusterInterface(); cluster != nil { + cluster.InvalidateCacheForChannelMembers(channelId) } } func InvalidateCacheForChannelSkipClusterSend(channelId string) { + Srv.Store.Channel().InvalidateChannel(channelId) +} + +func InvalidateCacheForChannelMembersSkipClusterSend(channelId string) { Srv.Store.User().InvalidateProfilesInChannelCache(channelId) Srv.Store.Channel().InvalidateMemberCount(channelId) - Srv.Store.Channel().InvalidateChannel(channelId) +} + +func InvalidateCacheForChannelByNameSkipClusterSend(teamId, name string) { + Srv.Store.Channel().InvalidateChannelByName(teamId, name) } func InvalidateCacheForChannelPosts(channelId string) { @@ -147,6 +164,18 @@ func InvalidateCacheForUserSkipClusterSend(userId string) { } } +func InvalidateCacheForWebhook(webhookId string) { + InvalidateCacheForWebhookSkipClusterSend(webhookId) + + if cluster := einterfaces.GetClusterInterface(); cluster != nil { + cluster.InvalidateCacheForWebhook(webhookId) + } +} + +func InvalidateCacheForWebhookSkipClusterSend(webhookId string) { + Srv.Store.Webhook().InvalidateWebhookCache(webhookId) +} + func InvalidateWebConnSessionCacheForUser(userId string) { if len(hubs) != 0 { GetHubForUserId(userId).InvalidateUser(userId) diff --git a/cmd/platform/channelargs.go b/cmd/platform/channelargs.go index ec697d86b..d64db10bd 100644 --- a/cmd/platform/channelargs.go +++ b/cmd/platform/channelargs.go @@ -42,7 +42,7 @@ func getChannelFromChannelArg(channelArg string) *model.Channel { return nil } - if result := <-app.Srv.Store.Channel().GetByNameIncludeDeleted(team.Id, channelPart); result.Err == nil { + if result := <-app.Srv.Store.Channel().GetByNameIncludeDeleted(team.Id, channelPart, true); result.Err == nil { channel = result.Data.(*model.Channel) } else { fmt.Println(result.Err.Error()) diff --git a/cmd/platform/oldcommands.go b/cmd/platform/oldcommands.go index ee7f66567..15ebb25ba 100644 --- a/cmd/platform/oldcommands.go +++ b/cmd/platform/oldcommands.go @@ -478,7 +478,7 @@ func cmdJoinChannel() { } var channel *model.Channel - if result := <-app.Srv.Store.Channel().GetByName(team.Id, flagChannelName); result.Err != nil { + if result := <-app.Srv.Store.Channel().GetByName(team.Id, flagChannelName, true); result.Err != nil { l4g.Error("%v", result.Err) flushLogAndExit(1) } else { @@ -539,7 +539,7 @@ func cmdLeaveChannel() { } var channel *model.Channel - if result := <-app.Srv.Store.Channel().GetByName(team.Id, flagChannelName); result.Err != nil { + if result := <-app.Srv.Store.Channel().GetByName(team.Id, flagChannelName, true); result.Err != nil { l4g.Error("%v", result.Err) flushLogAndExit(1) } else { diff --git a/einterfaces/cluster.go b/einterfaces/cluster.go index 6cf57308c..5ecee0b16 100644 --- a/einterfaces/cluster.go +++ b/einterfaces/cluster.go @@ -15,7 +15,10 @@ type ClusterInterface interface { ClearSessionCacheForUser(userId string) InvalidateCacheForUser(userId string) InvalidateCacheForChannel(channelId string) + InvalidateCacheForChannelByName(teamId, name string) + InvalidateCacheForChannelMembers(channelId string) InvalidateCacheForChannelPosts(channelId string) + InvalidateCacheForWebhook(webhookId string) Publish(event *model.WebSocketEvent) UpdateStatus(status *model.Status) GetLogs() ([]string, *model.AppError) diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index e3df07f74..7e90a6d27 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -38,11 +38,13 @@ type SqlChannelStore struct { var channelMemberCountsCache = utils.NewLru(CHANNEL_MEMBERS_COUNTS_CACHE_SIZE) var allChannelMembersForUserCache = utils.NewLru(ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SIZE) var channelCache = utils.NewLru(CHANNEL_CACHE_SIZE) +var channelByNameCache = utils.NewLru(CHANNEL_CACHE_SIZE) func ClearChannelCaches() { channelMemberCountsCache.Purge() allChannelMembersForUserCache.Purge() channelCache.Purge() + channelByNameCache.Purge() } func NewSqlChannelStore(sqlStore *SqlStore) ChannelStore { @@ -306,6 +308,10 @@ func (us SqlChannelStore) InvalidateChannel(id string) { channelCache.Remove(id) } +func (us SqlChannelStore) InvalidateChannelByName(teamId, name string) { + channelCache.Remove(teamId + name) +} + func (s SqlChannelStore) Get(id string, allowFromCache bool) StoreChannel { return s.get(id, false, allowFromCache) } @@ -539,15 +545,15 @@ func (s SqlChannelStore) GetTeamChannels(teamId string) StoreChannel { return storeChannel } -func (s SqlChannelStore) GetByName(teamId string, name string) StoreChannel { - return s.getByName(teamId, name, false) +func (s SqlChannelStore) GetByName(teamId string, name string, allowFromCache bool) StoreChannel { + return s.getByName(teamId, name, false, allowFromCache) } -func (s SqlChannelStore) GetByNameIncludeDeleted(teamId string, name string) StoreChannel { - return s.getByName(teamId, name, true) +func (s SqlChannelStore) GetByNameIncludeDeleted(teamId string, name string, allowFromCache bool) StoreChannel { + return s.getByName(teamId, name, true, allowFromCache) } -func (s SqlChannelStore) getByName(teamId string, name string, includeDeleted bool) StoreChannel { +func (s SqlChannelStore) getByName(teamId string, name string, includeDeleted bool, allowFromCache bool) StoreChannel { storeChannel := make(StoreChannel, 1) var query string @@ -562,6 +568,23 @@ func (s SqlChannelStore) getByName(teamId string, name string, includeDeleted bo channel := model.Channel{} + if allowFromCache { + metrics := einterfaces.GetMetricsInterface() + if cacheItem, ok := channelByNameCache.Get(teamId + name); ok { + if metrics != nil { + metrics.IncrementMemCacheHitCounter("Channel By Name") + } + result.Data = cacheItem.(*model.Channel) + storeChannel <- result + close(storeChannel) + return + } else { + if metrics != nil { + metrics.IncrementMemCacheMissCounter("Channel By Name") + } + } + } + if err := s.GetReplica().SelectOne(&channel, query, map[string]interface{}{"TeamId": teamId, "Name": name}); err != nil { if err == sql.ErrNoRows { result.Err = model.NewLocAppError("SqlChannelStore.GetByName", MISSING_CHANNEL_ERROR, nil, "teamId="+teamId+", "+"name="+name+", "+err.Error()) @@ -570,6 +593,7 @@ func (s SqlChannelStore) getByName(teamId string, name string, includeDeleted bo } } else { result.Data = &channel + channelByNameCache.AddWithExpiresInSecs(teamId+name, &channel, CHANNEL_CACHE_SEC) } storeChannel <- result diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index 5202a7c29..90a5f4479 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -352,7 +352,7 @@ func TestChannelStoreGetByName(t *testing.T) { o1.Type = model.CHANNEL_OPEN Must(store.Channel().Save(&o1)) - r1 := <-store.Channel().GetByName(o1.TeamId, o1.Name) + r1 := <-store.Channel().GetByName(o1.TeamId, o1.Name, true) if r1.Err != nil { t.Fatal(r1.Err) } else { @@ -361,13 +361,25 @@ func TestChannelStoreGetByName(t *testing.T) { } } - if err := (<-store.Channel().GetByName(o1.TeamId, "")).Err; err == nil { + if err := (<-store.Channel().GetByName(o1.TeamId, "", true)).Err; err == nil { + t.Fatal("Missing id should have failed") + } + + if r1 := <-store.Channel().GetByName(o1.TeamId, o1.Name, false); r1.Err != nil { + t.Fatal(r1.Err) + } else { + if r1.Data.(*model.Channel).ToJson() != o1.ToJson() { + t.Fatal("invalid returned channel") + } + } + + if err := (<-store.Channel().GetByName(o1.TeamId, "", false)).Err; err == nil { t.Fatal("Missing id should have failed") } Must(store.Channel().Delete(r1.Data.(*model.Channel).Id, model.GetMillis())) - if err := (<-store.Channel().GetByName(o1.TeamId, "")).Err; err == nil { + if err := (<-store.Channel().GetByName(o1.TeamId, "", false)).Err; err == nil { t.Fatal("Deleted channel should not be returned by GetByName()") } } 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 diff --git a/store/sql_webhook_store_test.go b/store/sql_webhook_store_test.go index 251ecf597..401b0f904 100644 --- a/store/sql_webhook_store_test.go +++ b/store/sql_webhook_store_test.go @@ -35,7 +35,7 @@ func TestWebhookStoreGetIncoming(t *testing.T) { o1 = (<-store.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) - if r1 := <-store.Webhook().GetIncoming(o1.Id); r1.Err != nil { + if r1 := <-store.Webhook().GetIncoming(o1.Id, false); r1.Err != nil { t.Fatal(r1.Err) } else { if r1.Data.(*model.IncomingWebhook).CreateAt != o1.CreateAt { @@ -43,7 +43,19 @@ func TestWebhookStoreGetIncoming(t *testing.T) { } } - if err := (<-store.Webhook().GetIncoming("123")).Err; err == nil { + if r1 := <-store.Webhook().GetIncoming(o1.Id, true); r1.Err != nil { + t.Fatal(r1.Err) + } else { + if r1.Data.(*model.IncomingWebhook).CreateAt != o1.CreateAt { + t.Fatal("invalid returned webhook") + } + } + + if err := (<-store.Webhook().GetIncoming("123", false)).Err; err == nil { + t.Fatal("Missing id should have failed") + } + + if err := (<-store.Webhook().GetIncoming("123", true)).Err; err == nil { t.Fatal("Missing id should have failed") } } @@ -85,7 +97,7 @@ func TestWebhookStoreDeleteIncoming(t *testing.T) { o1 = (<-store.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) - if r1 := <-store.Webhook().GetIncoming(o1.Id); r1.Err != nil { + if r1 := <-store.Webhook().GetIncoming(o1.Id, true); r1.Err != nil { t.Fatal(r1.Err) } else { if r1.Data.(*model.IncomingWebhook).CreateAt != o1.CreateAt { @@ -97,7 +109,9 @@ func TestWebhookStoreDeleteIncoming(t *testing.T) { t.Fatal(r2.Err) } - if r3 := (<-store.Webhook().GetIncoming(o1.Id)); r3.Err == nil { + ClearWebhookCaches() + + if r3 := (<-store.Webhook().GetIncoming(o1.Id, true)); r3.Err == nil { t.Log(r3.Data) t.Fatal("Missing id should have failed") } @@ -113,7 +127,7 @@ func TestWebhookStoreDeleteIncomingByUser(t *testing.T) { o1 = (<-store.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) - if r1 := <-store.Webhook().GetIncoming(o1.Id); r1.Err != nil { + if r1 := <-store.Webhook().GetIncoming(o1.Id, true); r1.Err != nil { t.Fatal(r1.Err) } else { if r1.Data.(*model.IncomingWebhook).CreateAt != o1.CreateAt { @@ -125,7 +139,9 @@ func TestWebhookStoreDeleteIncomingByUser(t *testing.T) { t.Fatal(r2.Err) } - if r3 := (<-store.Webhook().GetIncoming(o1.Id)); r3.Err == nil { + ClearWebhookCaches() + + if r3 := (<-store.Webhook().GetIncoming(o1.Id, true)); r3.Err == nil { t.Log(r3.Data) t.Fatal("Missing id should have failed") } diff --git a/store/store.go b/store/store.go index cd918c033..980ff7b1f 100644 --- a/store/store.go +++ b/store/store.go @@ -87,12 +87,13 @@ type ChannelStore interface { Update(channel *model.Channel) StoreChannel Get(id string, allowFromCache bool) StoreChannel InvalidateChannel(id string) + InvalidateChannelByName(teamId, name string) GetFromMaster(id string) StoreChannel Delete(channelId string, time int64) StoreChannel SetDeleteAt(channelId string, deleteAt int64, updateAt int64) StoreChannel PermanentDeleteByTeam(teamId string) StoreChannel - GetByName(team_id string, name string) StoreChannel - GetByNameIncludeDeleted(team_id string, name string) StoreChannel + GetByName(team_id string, name string, allowFromCache bool) StoreChannel + GetByNameIncludeDeleted(team_id string, name string, allowFromCache bool) StoreChannel GetDeletedByName(team_id string, name string) StoreChannel GetChannels(teamId string, userId string) StoreChannel GetMoreChannels(teamId string, userId string, offset int, limit int) StoreChannel @@ -245,7 +246,7 @@ type SystemStore interface { type WebhookStore interface { SaveIncoming(webhook *model.IncomingWebhook) StoreChannel - GetIncoming(id string) StoreChannel + GetIncoming(id string, allowFromCache bool) StoreChannel GetIncomingByTeam(teamId string) StoreChannel GetIncomingByChannel(channelId string) StoreChannel DeleteIncoming(webhookId string, time int64) StoreChannel @@ -259,6 +260,7 @@ type WebhookStore interface { UpdateOutgoing(hook *model.OutgoingWebhook) StoreChannel AnalyticsIncomingCount(teamId string) StoreChannel AnalyticsOutgoingCount(teamId string) StoreChannel + InvalidateWebhookCache(webhook string) } type CommandStore interface { -- cgit v1.2.3-1-g7c22