diff --git a/app/post.go b/app/post.go index 1d46a1b706..2cc8fbbbe5 100644 --- a/app/post.go +++ b/app/post.go @@ -607,7 +607,7 @@ func (a *App) GetPosts(channelId string, offset int, limit int) (*model.PostList } func (a *App) GetPostsEtag(channelId string) string { - return (<-a.Srv.Store.Post().GetEtag(channelId, true)).Data.(string) + return a.Srv.Store.Post().GetEtag(channelId, true) } func (a *App) GetPostsSince(channelId string, time int64) (*model.PostList, *model.AppError) { diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index 73bd0d36e5..bc28ff39f9 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -331,36 +331,35 @@ func (s *SqlPostStore) InvalidateLastPostTimeCache(channelId string) { } } -func (s *SqlPostStore) GetEtag(channelId string, allowFromCache bool) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - if allowFromCache { - if cacheItem, ok := s.lastPostTimeCache.Get(channelId); ok { - if s.metrics != nil { - s.metrics.IncrementMemCacheHitCounter("Last Post Time") - } - result.Data = fmt.Sprintf("%v.%v", model.CurrentVersion, cacheItem.(int64)) - return - } else { - if s.metrics != nil { - s.metrics.IncrementMemCacheMissCounter("Last Post Time") - } +func (s *SqlPostStore) GetEtag(channelId string, allowFromCache bool) string { + if allowFromCache { + if cacheItem, ok := s.lastPostTimeCache.Get(channelId); ok { + if s.metrics != nil { + s.metrics.IncrementMemCacheHitCounter("Last Post Time") } + return fmt.Sprintf("%v.%v", model.CurrentVersion, cacheItem.(int64)) } else { if s.metrics != nil { s.metrics.IncrementMemCacheMissCounter("Last Post Time") } } - - var et etagPosts - err := s.GetReplica().SelectOne(&et, "SELECT Id, UpdateAt FROM Posts WHERE ChannelId = :ChannelId ORDER BY UpdateAt DESC LIMIT 1", map[string]interface{}{"ChannelId": channelId}) - if err != nil { - result.Data = fmt.Sprintf("%v.%v", model.CurrentVersion, model.GetMillis()) - } else { - result.Data = fmt.Sprintf("%v.%v", model.CurrentVersion, et.UpdateAt) + } else { + if s.metrics != nil { + s.metrics.IncrementMemCacheMissCounter("Last Post Time") } + } - s.lastPostTimeCache.AddWithExpiresInSecs(channelId, et.UpdateAt, LAST_POST_TIME_CACHE_SEC) - }) + var et etagPosts + err := s.GetReplica().SelectOne(&et, "SELECT Id, UpdateAt FROM Posts WHERE ChannelId = :ChannelId ORDER BY UpdateAt DESC LIMIT 1", map[string]interface{}{"ChannelId": channelId}) + var result string + if err != nil { + result = fmt.Sprintf("%v.%v", model.CurrentVersion, model.GetMillis()) + } else { + result = fmt.Sprintf("%v.%v", model.CurrentVersion, et.UpdateAt) + } + + s.lastPostTimeCache.AddWithExpiresInSecs(channelId, et.UpdateAt, LAST_POST_TIME_CACHE_SEC) + return result } func (s *SqlPostStore) Delete(postId string, time int64, deleteByID string) *model.AppError { diff --git a/store/store.go b/store/store.go index f10a633816..0f3aa8205a 100644 --- a/store/store.go +++ b/store/store.go @@ -224,7 +224,7 @@ type PostStore interface { GetPostsBefore(channelId string, postId string, numPosts int, offset int) StoreChannel GetPostsAfter(channelId string, postId string, numPosts int, offset int) StoreChannel GetPostsSince(channelId string, time int64, allowFromCache bool) StoreChannel - GetEtag(channelId string, allowFromCache bool) StoreChannel + GetEtag(channelId string, allowFromCache bool) string Search(teamId string, userId string, params *model.SearchParams) StoreChannel AnalyticsUserCountsWithPostsByDay(teamId string) StoreChannel AnalyticsPostCountsByDay(teamId string) StoreChannel diff --git a/store/storetest/mocks/PostStore.go b/store/storetest/mocks/PostStore.go index ad2cda9497..b5b3194707 100644 --- a/store/storetest/mocks/PostStore.go +++ b/store/storetest/mocks/PostStore.go @@ -133,16 +133,14 @@ func (_m *PostStore) GetDirectPostParentsForExportAfter(limit int, afterId strin } // GetEtag provides a mock function with given fields: channelId, allowFromCache -func (_m *PostStore) GetEtag(channelId string, allowFromCache bool) store.StoreChannel { +func (_m *PostStore) GetEtag(channelId string, allowFromCache bool) string { ret := _m.Called(channelId, allowFromCache) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string, bool) store.StoreChannel); ok { + var r0 string + if rf, ok := ret.Get(0).(func(string, bool) string); ok { r0 = rf(channelId, allowFromCache) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) - } + r0 = ret.Get(0).(string) } return r0 diff --git a/store/storetest/post_store.go b/store/storetest/post_store.go index 338bb128e1..ee64ead8f0 100644 --- a/store/storetest/post_store.go +++ b/store/storetest/post_store.go @@ -116,14 +116,14 @@ func testPostStoreGet(t *testing.T, ss store.Store) { o1.UserId = model.NewId() o1.Message = "zz" + model.NewId() + "b" - etag1 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string) + etag1 := ss.Post().GetEtag(o1.ChannelId, false) if strings.Index(etag1, model.CurrentVersion+".") != 0 { t.Fatal("Invalid Etag") } o1 = (<-ss.Post().Save(o1)).Data.(*model.Post) - etag2 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string) + etag2 := ss.Post().GetEtag(o1.ChannelId, false) if strings.Index(etag2, fmt.Sprintf("%v.%v", model.CurrentVersion, o1.UpdateAt)) != 0 { t.Fatal("Invalid Etag") } @@ -172,13 +172,13 @@ func testGetEtagCache(t *testing.T, ss store.Store) { o1.UserId = model.NewId() o1.Message = "zz" + model.NewId() + "b" - etag1 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string) + etag1 := ss.Post().GetEtag(o1.ChannelId, true) if strings.Index(etag1, model.CurrentVersion+".") != 0 { t.Fatal("Invalid Etag") } // This one should come from the cache - etag2 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string) + etag2 := ss.Post().GetEtag(o1.ChannelId, true) if strings.Index(etag2, model.CurrentVersion+".") != 0 { t.Fatal("Invalid Etag") } @@ -186,7 +186,7 @@ func testGetEtagCache(t *testing.T, ss store.Store) { o1 = (<-ss.Post().Save(o1)).Data.(*model.Post) // We have not invalidated the cache so this should be the same as above - etag3 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string) + etag3 := ss.Post().GetEtag(o1.ChannelId, true) if strings.Index(etag3, etag2) != 0 { t.Fatal("Invalid Etag") } @@ -194,7 +194,7 @@ func testGetEtagCache(t *testing.T, ss store.Store) { ss.Post().InvalidateLastPostTimeCache(o1.ChannelId) // Invalidated cache so we should get a good result - etag4 := (<-ss.Post().GetEtag(o1.ChannelId, true)).Data.(string) + etag4 := ss.Post().GetEtag(o1.ChannelId, true) if strings.Index(etag4, fmt.Sprintf("%v.%v", model.CurrentVersion, o1.UpdateAt)) != 0 { t.Fatal("Invalid Etag") } @@ -332,7 +332,7 @@ func testPostStoreDelete(t *testing.T, ss store.Store) { o1.Message = "zz" + model.NewId() + "b" deleteByID := model.NewId() - etag1 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string) + etag1 := ss.Post().GetEtag(o1.ChannelId, false) if strings.Index(etag1, model.CurrentVersion+".") != 0 { t.Fatal("Invalid Etag") } @@ -363,7 +363,7 @@ func testPostStoreDelete(t *testing.T, ss store.Store) { t.Fatal("Missing id should have failed") } - etag2 := (<-ss.Post().GetEtag(o1.ChannelId, false)).Data.(string) + etag2 := ss.Post().GetEtag(o1.ChannelId, false) if strings.Index(etag2, model.CurrentVersion+".") != 0 { t.Fatal("Invalid Etag") }