[MM 19840] Create a Cache Interface abstraction to support multiple cache backends (#13384)
* Refactor to use structured logging * Properly formatted with gofmt * created interface Cache, but construction of cache is still coupled. * Implementing cache factories to build caches * Simple redis implementation without error handling. Keys and values by default are string * refactor NewLocalCacheLayer to inject cache factory * Removed redis impl to focus on cache abstraction * CacheFactory injected on sqlsupplier and saved in Store struct * remove useless private method * replace concrete declaration of lru cache to cache abstraction * discard spaces * Renamed factory to provider because concrete implementations of factories may hold an state (such as a redis client for example) * refactor to include all caches in the same package cache and subpackages * method close cache. This method will be used for concrete implementations that need to release resources (close a connection, etc) * closing cacheprovider and releasing resources while closing sql store * fixed merge conflict fail * gofmt files * remove unused property from post_store * naming refactor to avoid stutter. Added godocs on interface * Store doesnt know anything about the cache and provider. Cache provider will be built after loading config and injected in localCacheLayer * fixed broken test * cache provider initialized before RunOldAppInitialization which initializes the localcachelayer * move statusCache to server to initialize it with the new cache provider * update terms_service and channel_layer to have new cacheProvider * gofmt * Add Connect method to the cache provider * mock cacheprovider in user_layer_test Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
Jesús Espino
родитель
1909fd6607
Коммит
62b57143c8
@@ -150,7 +150,7 @@ func (a *App) InvalidateAllCaches() *model.AppError {
|
||||
func (a *App) InvalidateAllCachesSkipSend() {
|
||||
mlog.Info("Purging all caches")
|
||||
a.Srv.sessionCache.Purge()
|
||||
ClearStatusCache()
|
||||
a.Srv.statusCache.Purge()
|
||||
a.Srv.Store.Team().ClearCaches()
|
||||
a.Srv.Store.Channel().ClearCaches()
|
||||
a.Srv.Store.User().ClearCaches()
|
||||
|
||||
@@ -645,7 +645,7 @@ func (a *App) getMentionKeywordsInChannel(profiles map[string]*model.User, allow
|
||||
keywords,
|
||||
profile,
|
||||
channelMemberNotifyPropsMap[profile.Id],
|
||||
GetStatusFromCache(profile.Id),
|
||||
a.GetStatusFromCache(profile.Id),
|
||||
allowChannelMentions,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/dyatlov/go-opengraph/opengraph"
|
||||
"github.com/mattermost/mattermost-server/v5/mlog"
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/utils"
|
||||
"github.com/mattermost/mattermost-server/v5/services/cache/lru"
|
||||
"github.com/mattermost/mattermost-server/v5/utils/imgutils"
|
||||
"github.com/mattermost/mattermost-server/v5/utils/markdown"
|
||||
)
|
||||
@@ -24,7 +24,7 @@ const LINK_CACHE_SIZE = 10000
|
||||
const LINK_CACHE_DURATION = 3600
|
||||
const MaxMetadataImageSize = MaxOpenGraphResponseSize
|
||||
|
||||
var linkCache = utils.NewLru(LINK_CACHE_SIZE)
|
||||
var linkCache = lru.New(LINK_CACHE_SIZE)
|
||||
|
||||
func (a *App) InitPostMetadata() {
|
||||
// Dump any cached links if the proxy settings have changed so image URLs can be updated
|
||||
|
||||
@@ -30,6 +30,8 @@ import (
|
||||
"github.com/mattermost/mattermost-server/v5/mlog"
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/plugin"
|
||||
"github.com/mattermost/mattermost-server/v5/services/cache"
|
||||
"github.com/mattermost/mattermost-server/v5/services/cache/lru"
|
||||
"github.com/mattermost/mattermost-server/v5/services/httpservice"
|
||||
"github.com/mattermost/mattermost-server/v5/services/imageproxy"
|
||||
"github.com/mattermost/mattermost-server/v5/services/timezones"
|
||||
@@ -86,8 +88,9 @@ type Server struct {
|
||||
newStore func() store.Store
|
||||
|
||||
htmlTemplateWatcher *utils.HTMLTemplateWatcher
|
||||
sessionCache *utils.Cache
|
||||
seenPendingPostIdsCache *utils.Cache
|
||||
sessionCache cache.Cache
|
||||
seenPendingPostIdsCache cache.Cache
|
||||
statusCache cache.Cache
|
||||
configListenerId string
|
||||
licenseListenerId string
|
||||
logListenerId string
|
||||
@@ -129,18 +132,18 @@ type Server struct {
|
||||
Metrics einterfaces.MetricsInterface
|
||||
Notification einterfaces.NotificationInterface
|
||||
Saml einterfaces.SamlInterface
|
||||
|
||||
CacheProvider cache.Provider
|
||||
}
|
||||
|
||||
func NewServer(options ...Option) (*Server, error) {
|
||||
rootRouter := mux.NewRouter()
|
||||
|
||||
s := &Server{
|
||||
goroutineExitSignal: make(chan struct{}, 1),
|
||||
RootRouter: rootRouter,
|
||||
licenseListeners: map[string]func(){},
|
||||
sessionCache: utils.NewLru(model.SESSION_CACHE_SIZE),
|
||||
seenPendingPostIdsCache: utils.NewLru(PENDING_POST_IDS_CACHE_SIZE),
|
||||
clientConfig: make(map[string]string),
|
||||
goroutineExitSignal: make(chan struct{}, 1),
|
||||
RootRouter: rootRouter,
|
||||
licenseListeners: map[string]func(){},
|
||||
clientConfig: make(map[string]string),
|
||||
}
|
||||
for _, option := range options {
|
||||
if err := option(s); err != nil {
|
||||
@@ -188,6 +191,16 @@ func NewServer(options ...Option) (*Server, error) {
|
||||
return nil, errors.Wrapf(err, "unable to load Mattermost translation files")
|
||||
}
|
||||
|
||||
// at the moment we only have this implementation
|
||||
// in the future the cache provider will be built based on the loaded config
|
||||
s.CacheProvider = new(lru.CacheProvider)
|
||||
|
||||
s.CacheProvider.Connect()
|
||||
|
||||
s.sessionCache = s.CacheProvider.NewCache(model.SESSION_CACHE_SIZE)
|
||||
s.seenPendingPostIdsCache = s.CacheProvider.NewCache(PENDING_POST_IDS_CACHE_SIZE)
|
||||
s.statusCache = s.CacheProvider.NewCache(model.STATUS_CACHE_SIZE)
|
||||
|
||||
err := s.RunOldAppInitialization()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -394,6 +407,10 @@ func (s *Server) Shutdown() error {
|
||||
s.Store.Close()
|
||||
}
|
||||
|
||||
if s.CacheProvider != nil {
|
||||
s.CacheProvider.Close()
|
||||
}
|
||||
|
||||
mlog.Info("Server stopped")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ func (s *Server) RunOldAppInitialization() error {
|
||||
return store.NewTimerLayer(
|
||||
localcachelayer.NewLocalCacheLayer(
|
||||
sqlstore.NewSqlSupplier(s.FakeApp().Config().SqlSettings, s.Metrics),
|
||||
s.Metrics, s.Cluster),
|
||||
s.Metrics, s.Cluster, s.CacheProvider),
|
||||
s.Metrics)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,17 +6,10 @@ package app
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/v5/mlog"
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/utils"
|
||||
)
|
||||
|
||||
var statusCache *utils.Cache = utils.NewLru(model.STATUS_CACHE_SIZE)
|
||||
|
||||
func ClearStatusCache() {
|
||||
statusCache.Purge()
|
||||
}
|
||||
|
||||
func (a *App) AddStatusCacheSkipClusterSend(status *model.Status) {
|
||||
statusCache.Add(status.UserId, status)
|
||||
a.Srv.statusCache.Add(status.UserId, status)
|
||||
}
|
||||
|
||||
func (a *App) AddStatusCache(status *model.Status) {
|
||||
@@ -37,12 +30,12 @@ func (a *App) GetAllStatuses() map[string]*model.Status {
|
||||
return map[string]*model.Status{}
|
||||
}
|
||||
|
||||
userIds := statusCache.Keys()
|
||||
userIds := a.Srv.statusCache.Keys()
|
||||
statusMap := map[string]*model.Status{}
|
||||
|
||||
for _, userId := range userIds {
|
||||
if id, ok := userId.(string); ok {
|
||||
status := GetStatusFromCache(id)
|
||||
status := a.GetStatusFromCache(id)
|
||||
if status != nil {
|
||||
statusMap[id] = status
|
||||
}
|
||||
@@ -62,7 +55,7 @@ func (a *App) GetStatusesByIds(userIds []string) (map[string]interface{}, *model
|
||||
|
||||
missingUserIds := []string{}
|
||||
for _, userId := range userIds {
|
||||
if result, ok := statusCache.Get(userId); ok {
|
||||
if result, ok := a.Srv.statusCache.Get(userId); ok {
|
||||
statusMap[userId] = result.(*model.Status).Status
|
||||
if metrics != nil {
|
||||
metrics.IncrementMemCacheHitCounter("Status")
|
||||
@@ -109,7 +102,7 @@ func (a *App) GetUserStatusesByIds(userIds []string) ([]*model.Status, *model.Ap
|
||||
|
||||
missingUserIds := []string{}
|
||||
for _, userId := range userIds {
|
||||
if result, ok := statusCache.Get(userId); ok {
|
||||
if result, ok := a.Srv.statusCache.Get(userId); ok {
|
||||
statusMap = append(statusMap, result.(*model.Status))
|
||||
if metrics != nil {
|
||||
metrics.IncrementMemCacheHitCounter("Status")
|
||||
@@ -329,8 +322,8 @@ func (a *App) SetStatusOutOfOffice(userId string) {
|
||||
a.SaveAndBroadcastStatus(status)
|
||||
}
|
||||
|
||||
func GetStatusFromCache(userId string) *model.Status {
|
||||
if result, ok := statusCache.Get(userId); ok {
|
||||
func (a *App) GetStatusFromCache(userId string) *model.Status {
|
||||
if result, ok := a.Srv.statusCache.Get(userId); ok {
|
||||
status := result.(*model.Status)
|
||||
statusCopy := &model.Status{}
|
||||
*statusCopy = *status
|
||||
@@ -345,7 +338,7 @@ func (a *App) GetStatus(userId string) (*model.Status, *model.AppError) {
|
||||
return &model.Status{}, nil
|
||||
}
|
||||
|
||||
status := GetStatusFromCache(userId)
|
||||
status := a.GetStatusFromCache(userId)
|
||||
if status != nil {
|
||||
return status, nil
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user