Adding caching of last 60 posts. (#4880)

Этот коммит содержится в:
Christopher Speller
2016-12-22 18:14:54 -05:00
коммит произвёл Corey Hulen
родитель ca61b4bed9
Коммит ffd6ccde2e
4 изменённых файлов: 62 добавлений и 4 удалений

Просмотреть файл

@@ -1295,7 +1295,7 @@ func getPosts(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
pchan := Srv.Store.Post().GetPosts(id, offset, limit) pchan := Srv.Store.Post().GetPosts(id, offset, limit, true)
if result := <-pchan; result.Err != nil { if result := <-pchan; result.Err != nil {
c.Err = result.Err c.Err = result.Err

Просмотреть файл

@@ -21,12 +21,17 @@ type SqlPostStore struct {
const ( const (
LAST_POST_TIME_CACHE_SIZE = 25000 LAST_POST_TIME_CACHE_SIZE = 25000
LAST_POST_TIME_CACHE_SEC = 900 // 15 minutes LAST_POST_TIME_CACHE_SEC = 900 // 15 minutes
LAST_POSTS_CACHE_SIZE = 1000
LAST_POSTS_CACHE_SEC = 900 // 15 minutes
) )
var lastPostTimeCache = utils.NewLru(LAST_POST_TIME_CACHE_SIZE) var lastPostTimeCache = utils.NewLru(LAST_POST_TIME_CACHE_SIZE)
var lastPostsCache = utils.NewLru(LAST_POSTS_CACHE_SIZE)
func ClearPostCaches() { func ClearPostCaches() {
lastPostTimeCache.Purge() lastPostTimeCache.Purge()
lastPostsCache.Purge()
} }
func NewSqlPostStore(sqlStore *SqlStore) PostStore { func NewSqlPostStore(sqlStore *SqlStore) PostStore {
@@ -224,6 +229,7 @@ type etagPosts struct {
func (s SqlPostStore) InvalidateLastPostTimeCache(channelId string) { func (s SqlPostStore) InvalidateLastPostTimeCache(channelId string) {
lastPostTimeCache.Remove(channelId) lastPostTimeCache.Remove(channelId)
lastPostsCache.Remove(channelId)
} }
func (s SqlPostStore) GetEtag(channelId string, allowFromCache bool) StoreChannel { func (s SqlPostStore) GetEtag(channelId string, allowFromCache bool) StoreChannel {
@@ -381,11 +387,12 @@ func (s SqlPostStore) PermanentDeleteByUser(userId string) StoreChannel {
return storeChannel return storeChannel
} }
func (s SqlPostStore) GetPosts(channelId string, offset int, limit int) StoreChannel { func (s SqlPostStore) GetPosts(channelId string, offset int, limit int, allowFromCache bool) StoreChannel {
storeChannel := make(StoreChannel, 1) storeChannel := make(StoreChannel, 1)
go func() { go func() {
result := StoreResult{} result := StoreResult{}
metrics := einterfaces.GetMetricsInterface()
if limit > 1000 { if limit > 1000 {
result.Err = model.NewLocAppError("SqlPostStore.GetLinearPosts", "store.sql_post.get_posts.app_error", nil, "channelId="+channelId) result.Err = model.NewLocAppError("SqlPostStore.GetLinearPosts", "store.sql_post.get_posts.app_error", nil, "channelId="+channelId)
@@ -394,6 +401,27 @@ func (s SqlPostStore) GetPosts(channelId string, offset int, limit int) StoreCha
return return
} }
if allowFromCache && offset == 0 && limit == 60 {
if cacheItem, ok := lastPostsCache.Get(channelId); ok {
if metrics != nil {
metrics.IncrementMemCacheHitCounter("Last Posts Cache")
}
result.Data = cacheItem.(*model.PostList)
storeChannel <- result
close(storeChannel)
return
} else {
if metrics != nil {
metrics.IncrementMemCacheMissCounter("Last Posts Cache")
}
}
} else {
if metrics != nil {
metrics.IncrementMemCacheMissCounter("Last Posts Cache")
}
}
rpc := s.getRootPosts(channelId, offset, limit) rpc := s.getRootPosts(channelId, offset, limit)
cpc := s.getParentsPosts(channelId, offset, limit) cpc := s.getParentsPosts(channelId, offset, limit)
@@ -418,6 +446,10 @@ func (s SqlPostStore) GetPosts(channelId string, offset int, limit int) StoreCha
list.MakeNonNil() list.MakeNonNil()
if offset == 0 && limit == 60 {
lastPostsCache.AddWithExpiresInSecs(channelId, list, LAST_POSTS_CACHE_SEC)
}
result.Data = list result.Data = list
} }

Просмотреть файл

@@ -491,7 +491,7 @@ func TestPostStoreGetPostsWtihDetails(t *testing.T) {
o5.RootId = o4.Id o5.RootId = o4.Id
o5 = (<-store.Post().Save(o5)).Data.(*model.Post) o5 = (<-store.Post().Save(o5)).Data.(*model.Post)
r1 := (<-store.Post().GetPosts(o1.ChannelId, 0, 4)).Data.(*model.PostList) r1 := (<-store.Post().GetPosts(o1.ChannelId, 0, 4, false)).Data.(*model.PostList)
if r1.Order[0] != o5.Id { if r1.Order[0] != o5.Id {
t.Fatal("invalid order") t.Fatal("invalid order")
@@ -516,6 +516,32 @@ func TestPostStoreGetPostsWtihDetails(t *testing.T) {
if r1.Posts[o1.Id].Message != o1.Message { if r1.Posts[o1.Id].Message != o1.Message {
t.Fatal("Missing parent") t.Fatal("Missing parent")
} }
r2 := (<-store.Post().GetPosts(o1.ChannelId, 0, 4, true)).Data.(*model.PostList)
if r2.Order[0] != o5.Id {
t.Fatal("invalid order")
}
if r2.Order[1] != o4.Id {
t.Fatal("invalid order")
}
if r2.Order[2] != o3.Id {
t.Fatal("invalid order")
}
if r2.Order[3] != o2a.Id {
t.Fatal("invalid order")
}
if len(r2.Posts) != 6 { //the last 4, + o1 (o2a and o3's parent) + o2 (in same thread as o2a and o3)
t.Fatal("wrong size")
}
if r2.Posts[o1.Id].Message != o1.Message {
t.Fatal("Missing parent")
}
} }
func TestPostStoreGetPostsBeforeAfter(t *testing.T) { func TestPostStoreGetPostsBeforeAfter(t *testing.T) {

Просмотреть файл

@@ -127,7 +127,7 @@ type PostStore interface {
Get(id string) StoreChannel Get(id string) StoreChannel
Delete(postId string, time int64) StoreChannel Delete(postId string, time int64) StoreChannel
PermanentDeleteByUser(userId string) StoreChannel PermanentDeleteByUser(userId string) StoreChannel
GetPosts(channelId string, offset int, limit int) StoreChannel GetPosts(channelId string, offset int, limit int, allowFromCache bool) StoreChannel
GetFlaggedPosts(userId string, offset int, limit int) StoreChannel GetFlaggedPosts(userId string, offset int, limit int) StoreChannel
GetPostsBefore(channelId string, postId string, numPosts int, offset int) StoreChannel GetPostsBefore(channelId string, postId string, numPosts int, offset int) StoreChannel
GetPostsAfter(channelId string, postId string, numPosts int, offset int) StoreChannel GetPostsAfter(channelId string, postId string, numPosts int, offset int) StoreChannel