Adding caching layer to some posts calls (#4779)
Этот коммит содержится в:
коммит произвёл
enahum
родитель
2e58f7504b
Коммит
86fb0d87a3
@@ -17,6 +17,17 @@ type SqlPostStore struct {
|
||||
*SqlStore
|
||||
}
|
||||
|
||||
const (
|
||||
POSTS_ETAG_CACHE_SIZE = 25000
|
||||
POSTS_ETAG_CACHE_SEC = 900 // 15 minutes
|
||||
)
|
||||
|
||||
var postEtagCache = utils.NewLru(CHANNEL_MEMBERS_COUNTS_CACHE_SIZE)
|
||||
|
||||
func ClearPostCaches() {
|
||||
postEtagCache.Purge()
|
||||
}
|
||||
|
||||
func NewSqlPostStore(sqlStore *SqlStore) PostStore {
|
||||
s := &SqlPostStore{sqlStore}
|
||||
|
||||
@@ -210,12 +221,25 @@ type etagPosts struct {
|
||||
UpdateAt int64
|
||||
}
|
||||
|
||||
func (s SqlPostStore) GetEtag(channelId string) StoreChannel {
|
||||
func (s SqlPostStore) InvalidatePostEtagCache(channelId string) {
|
||||
postEtagCache.Remove(channelId)
|
||||
}
|
||||
|
||||
func (s SqlPostStore) GetEtag(channelId string, allowFromCache bool) StoreChannel {
|
||||
storeChannel := make(StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := StoreResult{}
|
||||
|
||||
if allowFromCache {
|
||||
if cacheItem, ok := postEtagCache.Get(channelId); ok {
|
||||
result.Data = cacheItem.(string)
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -224,6 +248,8 @@ func (s SqlPostStore) GetEtag(channelId string) StoreChannel {
|
||||
result.Data = fmt.Sprintf("%v.%v.%v", model.CurrentVersion, et.Id, et.UpdateAt)
|
||||
}
|
||||
|
||||
postEtagCache.AddWithExpiresInSecs(channelId, result.Data.(string), POSTS_ETAG_CACHE_SEC)
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
@@ -37,14 +37,14 @@ func TestPostStoreGet(t *testing.T) {
|
||||
o1.UserId = model.NewId()
|
||||
o1.Message = "a" + model.NewId() + "b"
|
||||
|
||||
etag1 := (<-store.Post().GetEtag(o1.ChannelId)).Data.(string)
|
||||
etag1 := (<-store.Post().GetEtag(o1.ChannelId, false)).Data.(string)
|
||||
if strings.Index(etag1, model.CurrentVersion+".0.") != 0 {
|
||||
t.Fatal("Invalid Etag")
|
||||
}
|
||||
|
||||
o1 = (<-store.Post().Save(o1)).Data.(*model.Post)
|
||||
|
||||
etag2 := (<-store.Post().GetEtag(o1.ChannelId)).Data.(string)
|
||||
etag2 := (<-store.Post().GetEtag(o1.ChannelId, false)).Data.(string)
|
||||
if strings.Index(etag2, model.CurrentVersion+"."+o1.Id) != 0 {
|
||||
t.Fatal("Invalid Etag")
|
||||
}
|
||||
@@ -62,6 +62,41 @@ func TestPostStoreGet(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEtagCache(t *testing.T) {
|
||||
Setup()
|
||||
o1 := &model.Post{}
|
||||
o1.ChannelId = model.NewId()
|
||||
o1.UserId = model.NewId()
|
||||
o1.Message = "a" + model.NewId() + "b"
|
||||
|
||||
etag1 := (<-store.Post().GetEtag(o1.ChannelId, true)).Data.(string)
|
||||
if strings.Index(etag1, model.CurrentVersion+".0.") != 0 {
|
||||
t.Fatal("Invalid Etag")
|
||||
}
|
||||
|
||||
// This one should come from the cache
|
||||
etag2 := (<-store.Post().GetEtag(o1.ChannelId, true)).Data.(string)
|
||||
if strings.Index(etag2, model.CurrentVersion+".0.") != 0 {
|
||||
t.Fatal("Invalid Etag")
|
||||
}
|
||||
|
||||
o1 = (<-store.Post().Save(o1)).Data.(*model.Post)
|
||||
|
||||
// We have not invalidated the cache so this should be the same as above
|
||||
etag3 := (<-store.Post().GetEtag(o1.ChannelId, true)).Data.(string)
|
||||
if strings.Index(etag3, model.CurrentVersion+".0.") != 0 {
|
||||
t.Fatal("Invalid Etag")
|
||||
}
|
||||
|
||||
store.Post().InvalidatePostEtagCache(o1.ChannelId)
|
||||
|
||||
// Invalidated cache so we should get a good result
|
||||
etag4 := (<-store.Post().GetEtag(o1.ChannelId, true)).Data.(string)
|
||||
if strings.Index(etag4, model.CurrentVersion+"."+o1.Id) != 0 {
|
||||
t.Fatal("Invalid Etag")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostStoreUpdate(t *testing.T) {
|
||||
Setup()
|
||||
|
||||
@@ -164,7 +199,7 @@ func TestPostStoreDelete(t *testing.T) {
|
||||
o1.UserId = model.NewId()
|
||||
o1.Message = "a" + model.NewId() + "b"
|
||||
|
||||
etag1 := (<-store.Post().GetEtag(o1.ChannelId)).Data.(string)
|
||||
etag1 := (<-store.Post().GetEtag(o1.ChannelId, false)).Data.(string)
|
||||
if strings.Index(etag1, model.CurrentVersion+".0.") != 0 {
|
||||
t.Fatal("Invalid Etag")
|
||||
}
|
||||
@@ -188,7 +223,7 @@ func TestPostStoreDelete(t *testing.T) {
|
||||
t.Fatal("Missing id should have failed")
|
||||
}
|
||||
|
||||
etag2 := (<-store.Post().GetEtag(o1.ChannelId)).Data.(string)
|
||||
etag2 := (<-store.Post().GetEtag(o1.ChannelId, false)).Data.(string)
|
||||
if strings.Index(etag2, model.CurrentVersion+"."+o1.Id) != 0 {
|
||||
t.Fatal("Invalid Etag")
|
||||
}
|
||||
|
||||
@@ -130,11 +130,12 @@ 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) StoreChannel
|
||||
GetEtag(channelId string) StoreChannel
|
||||
GetEtag(channelId string, allowFromCache bool) StoreChannel
|
||||
Search(teamId string, userId string, params *model.SearchParams) StoreChannel
|
||||
AnalyticsUserCountsWithPostsByDay(teamId string) StoreChannel
|
||||
AnalyticsPostCountsByDay(teamId string) StoreChannel
|
||||
AnalyticsPostCount(teamId string, mustHaveFile bool, mustHaveHashtag bool) StoreChannel
|
||||
InvalidatePostEtagCache(channelId string)
|
||||
}
|
||||
|
||||
type UserStore interface {
|
||||
|
||||
Ссылка в новой задаче
Block a user