mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Add caching for file infos (#5330)
This commit is contained in:
@@ -3,13 +3,26 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"github.com/mattermost/platform/einterfaces"
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/utils"
|
||||
)
|
||||
|
||||
type SqlFileInfoStore struct {
|
||||
*SqlStore
|
||||
}
|
||||
|
||||
const (
|
||||
FILE_INFO_CACHE_SIZE = 25000
|
||||
FILE_INFO_CACHE_SEC = 900 // 15 minutes
|
||||
)
|
||||
|
||||
var fileInfoCache *utils.Cache = utils.NewLru(FILE_INFO_CACHE_SIZE)
|
||||
|
||||
func ClearFileCaches() {
|
||||
fileInfoCache.Purge()
|
||||
}
|
||||
|
||||
func NewSqlFileInfoStore(sqlStore *SqlStore) FileInfoStore {
|
||||
s := &SqlFileInfoStore{sqlStore}
|
||||
|
||||
@@ -119,12 +132,39 @@ func (fs SqlFileInfoStore) GetByPath(path string) StoreChannel {
|
||||
return storeChannel
|
||||
}
|
||||
|
||||
func (fs SqlFileInfoStore) GetForPost(postId string) StoreChannel {
|
||||
func (s SqlFileInfoStore) InvalidateFileInfosForPostCache(postId string) {
|
||||
fileInfoCache.Remove(postId)
|
||||
}
|
||||
|
||||
func (fs SqlFileInfoStore) GetForPost(postId string, allowFromCache bool) StoreChannel {
|
||||
storeChannel := make(StoreChannel, 1)
|
||||
|
||||
go func() {
|
||||
result := StoreResult{}
|
||||
|
||||
metrics := einterfaces.GetMetricsInterface()
|
||||
|
||||
if allowFromCache {
|
||||
if cacheItem, ok := fileInfoCache.Get(postId); ok {
|
||||
if metrics != nil {
|
||||
metrics.IncrementMemCacheHitCounter("File Info Cache")
|
||||
}
|
||||
|
||||
result.Data = cacheItem.([]*model.FileInfo)
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
return
|
||||
} else {
|
||||
if metrics != nil {
|
||||
metrics.IncrementMemCacheMissCounter("File Info Cache")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if metrics != nil {
|
||||
metrics.IncrementMemCacheMissCounter("File Info Cache")
|
||||
}
|
||||
}
|
||||
|
||||
var infos []*model.FileInfo
|
||||
|
||||
if _, err := fs.GetReplica().Select(&infos,
|
||||
@@ -140,6 +180,7 @@ func (fs SqlFileInfoStore) GetForPost(postId string) StoreChannel {
|
||||
result.Err = model.NewLocAppError("SqlFileInfoStore.GetForPost",
|
||||
"store.sql_file_info.get_for_post.app_error", nil, "post_id="+postId+", "+err.Error())
|
||||
} else {
|
||||
fileInfoCache.AddWithExpiresInSecs(postId, infos, FILE_INFO_CACHE_SEC)
|
||||
result.Data = infos
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user