Adding caching layer to some posts calls (#4779)

Этот коммит содержится в:
Christopher Speller
2016-12-13 22:24:24 -05:00
коммит произвёл enahum
родитель 2e58f7504b
Коммит 86fb0d87a3
9 изменённых файлов: 102 добавлений и 9 удалений

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

@@ -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)
}()