From 274b9b6032572dd33b28815a9c13bb18a02becbe Mon Sep 17 00:00:00 2001 From: Corey Hulen Date: Mon, 20 Feb 2017 13:40:32 -0500 Subject: Fixing file info caching issue with Aurora for master (#5477) --- store/sql_file_info_store.go | 15 ++++++++++++--- store/sql_file_info_store_test.go | 14 ++++++++++---- store/store.go | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) (limited to 'store') diff --git a/store/sql_file_info_store.go b/store/sql_file_info_store.go index 467ae39aa..bd0362db0 100644 --- a/store/sql_file_info_store.go +++ b/store/sql_file_info_store.go @@ -143,7 +143,7 @@ func (s SqlFileInfoStore) InvalidateFileInfosForPostCache(postId string) { fileInfoCache.Remove(postId) } -func (fs SqlFileInfoStore) GetForPost(postId string, allowFromCache bool) StoreChannel { +func (fs SqlFileInfoStore) GetForPost(postId string, readFromMaster bool, allowFromCache bool) StoreChannel { storeChannel := make(StoreChannel, 1) go func() { @@ -174,7 +174,13 @@ func (fs SqlFileInfoStore) GetForPost(postId string, allowFromCache bool) StoreC var infos []*model.FileInfo - if _, err := fs.GetReplica().Select(&infos, + dbmap := fs.GetReplica() + + if readFromMaster { + dbmap = fs.GetMaster() + } + + if _, err := dbmap.Select(&infos, `SELECT * FROM @@ -187,7 +193,10 @@ func (fs SqlFileInfoStore) GetForPost(postId string, allowFromCache bool) StoreC result.Err = model.NewLocAppError("SqlFileInfoStore.GetForPost", "store.sql_file_info.get_for_post.app_error", nil, "post_id="+postId+", "+err.Error()) } else { - fileInfoCache.AddWithExpiresInSecs(postId, infos, FILE_INFO_CACHE_SEC) + if len(infos) > 0 { + fileInfoCache.AddWithExpiresInSecs(postId, infos, FILE_INFO_CACHE_SEC) + } + result.Data = infos } diff --git a/store/sql_file_info_store_test.go b/store/sql_file_info_store_test.go index 672e15ef2..5e4d0e66e 100644 --- a/store/sql_file_info_store_test.go +++ b/store/sql_file_info_store_test.go @@ -114,13 +114,19 @@ func TestFileInfoGetForPost(t *testing.T) { infos[i] = Must(store.FileInfo().Save(info)).(*model.FileInfo) } - if result := <-store.FileInfo().GetForPost(postId, false); result.Err != nil { + if result := <-store.FileInfo().GetForPost(postId, true, false); result.Err != nil { t.Fatal(result.Err) } else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 { t.Fatal("should've returned exactly 2 file infos") } - if result := <-store.FileInfo().GetForPost(postId, true); result.Err != nil { + if result := <-store.FileInfo().GetForPost(postId, false, false); result.Err != nil { + t.Fatal(result.Err) + } else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 { + t.Fatal("should've returned exactly 2 file infos") + } + + if result := <-store.FileInfo().GetForPost(postId, true, true); result.Err != nil { t.Fatal(result.Err) } else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 { t.Fatal("should've returned exactly 2 file infos") @@ -163,7 +169,7 @@ func TestFileInfoAttachToPost(t *testing.T) { info2 = Must(store.FileInfo().Get(info2.Id)).(*model.FileInfo) } - if result := <-store.FileInfo().GetForPost(postId, false); result.Err != nil { + if result := <-store.FileInfo().GetForPost(postId, true, false); result.Err != nil { t.Fatal(result.Err) } else if infos := result.Data.([]*model.FileInfo); len(infos) != 2 { t.Fatal("should've returned exactly 2 file infos") @@ -208,7 +214,7 @@ func TestFileInfoDeleteForPost(t *testing.T) { t.Fatal(result.Err) } - if infos := Must(store.FileInfo().GetForPost(postId, false)).([]*model.FileInfo); len(infos) != 0 { + if infos := Must(store.FileInfo().GetForPost(postId, true, false)).([]*model.FileInfo); len(infos) != 0 { t.Fatal("shouldn't have returned any file infos") } } diff --git a/store/store.go b/store/store.go index 00866d523..1a43767dd 100644 --- a/store/store.go +++ b/store/store.go @@ -326,7 +326,7 @@ type FileInfoStore interface { Save(info *model.FileInfo) StoreChannel Get(id string) StoreChannel GetByPath(path string) StoreChannel - GetForPost(postId string, allowFromCache bool) StoreChannel + GetForPost(postId string, readFromMaster bool, allowFromCache bool) StoreChannel InvalidateFileInfosForPostCache(postId string) AttachToPost(fileId string, postId string) StoreChannel DeleteForPost(postId string) StoreChannel -- cgit v1.2.3-1-g7c22