mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Adding caching of last 60 posts. (#4880)
This commit is contained in:
committed by
Corey Hulen
parent
ca61b4bed9
commit
ffd6ccde2e
@@ -1295,7 +1295,7 @@ func getPosts(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
pchan := Srv.Store.Post().GetPosts(id, offset, limit)
|
||||
pchan := Srv.Store.Post().GetPosts(id, offset, limit, true)
|
||||
|
||||
if result := <-pchan; result.Err != nil {
|
||||
c.Err = result.Err
|
||||
|
||||
@@ -21,12 +21,17 @@ type SqlPostStore struct {
|
||||
const (
|
||||
LAST_POST_TIME_CACHE_SIZE = 25000
|
||||
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 lastPostsCache = utils.NewLru(LAST_POSTS_CACHE_SIZE)
|
||||
|
||||
func ClearPostCaches() {
|
||||
lastPostTimeCache.Purge()
|
||||
lastPostsCache.Purge()
|
||||
}
|
||||
|
||||
func NewSqlPostStore(sqlStore *SqlStore) PostStore {
|
||||
@@ -224,6 +229,7 @@ type etagPosts struct {
|
||||
|
||||
func (s SqlPostStore) InvalidateLastPostTimeCache(channelId string) {
|
||||
lastPostTimeCache.Remove(channelId)
|
||||
lastPostsCache.Remove(channelId)
|
||||
}
|
||||
|
||||
func (s SqlPostStore) GetEtag(channelId string, allowFromCache bool) StoreChannel {
|
||||
@@ -381,11 +387,12 @@ func (s SqlPostStore) PermanentDeleteByUser(userId string) 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)
|
||||
|
||||
go func() {
|
||||
result := StoreResult{}
|
||||
metrics := einterfaces.GetMetricsInterface()
|
||||
|
||||
if limit > 1000 {
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
cpc := s.getParentsPosts(channelId, offset, limit)
|
||||
|
||||
@@ -418,6 +446,10 @@ func (s SqlPostStore) GetPosts(channelId string, offset int, limit int) StoreCha
|
||||
|
||||
list.MakeNonNil()
|
||||
|
||||
if offset == 0 && limit == 60 {
|
||||
lastPostsCache.AddWithExpiresInSecs(channelId, list, LAST_POSTS_CACHE_SEC)
|
||||
}
|
||||
|
||||
result.Data = list
|
||||
}
|
||||
|
||||
|
||||
@@ -491,7 +491,7 @@ func TestPostStoreGetPostsWtihDetails(t *testing.T) {
|
||||
o5.RootId = o4.Id
|
||||
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 {
|
||||
t.Fatal("invalid order")
|
||||
@@ -516,6 +516,32 @@ func TestPostStoreGetPostsWtihDetails(t *testing.T) {
|
||||
if r1.Posts[o1.Id].Message != o1.Message {
|
||||
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) {
|
||||
|
||||
@@ -127,7 +127,7 @@ type PostStore interface {
|
||||
Get(id string) StoreChannel
|
||||
Delete(postId string, time int64) 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
|
||||
GetPostsBefore(channelId string, postId string, numPosts int, offset int) StoreChannel
|
||||
GetPostsAfter(channelId string, postId string, numPosts int, offset int) StoreChannel
|
||||
|
||||
Reference in New Issue
Block a user