Files
team/server/channels/store/localcachelayer/session_attribute_layer.go
Devin BinnieandGitHub 6554833b26 [MM-68988][MM-68989][MM-68990][MM-68991][MM-68997][MM-68998] Session Attributes MVF - Server-work (#36934)
* [MM-68988][MM-68989][MM-68990][MM-68991][MM-68997] Session Attributes MVF - Server-work

* PR feedback

* [MM-68998] Add web app hooks for Desktop App to signal a refresh of attributes/manifest

* Fix types

* Adjust the test to test the license first

* PR feedback

* Coderabbit feedback

* More tests
2026-06-09 13:10:53 -04:00

90 lines
2.6 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package localcachelayer
import (
"bytes"
"maps"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/v8/channels/store"
"github.com/mattermost/mattermost/server/v8/platform/services/cache"
)
type sessionAttributeEntry struct {
Attrs map[string]any
Timestamps map[string]int64
}
type LocalCacheSessionAttributeStore struct {
store.SessionAttributeStore
rootStore *LocalCacheStore
}
func (s *LocalCacheSessionAttributeStore) handleClusterInvalidateSessionAttributes(msg *model.ClusterMessage) {
if bytes.Equal(msg.Data, clearCacheMessageData) {
err := s.rootStore.sessionAttributeCache.Purge()
if err != nil {
s.rootStore.logger.Warn("Error while purging session attribute cache", mlog.Err(err))
}
} else {
err := s.rootStore.sessionAttributeCache.Remove(string(msg.Data))
if err != nil {
s.rootStore.logger.Warn("Error while removing session attribute cache", mlog.Err(err))
}
}
}
func (s *LocalCacheSessionAttributeStore) Refresh(sessionID string, attrs map[string]any, updatedAt int64) error {
if len(attrs) == 0 {
return nil
}
entry := &sessionAttributeEntry{
Attrs: make(map[string]any, len(attrs)),
Timestamps: make(map[string]int64, len(attrs)),
}
var existing *sessionAttributeEntry
if err := s.rootStore.doStandardReadCache(s.rootStore.sessionAttributeCache, sessionID, &existing); err == nil && existing != nil {
maps.Copy(entry.Attrs, existing.Attrs)
maps.Copy(entry.Timestamps, existing.Timestamps)
}
for key, value := range attrs {
if entry.Timestamps[key] > updatedAt {
continue
}
entry.Attrs[key] = value
entry.Timestamps[key] = updatedAt
}
s.rootStore.doStandardAddToCache(s.rootStore.sessionAttributeCache, sessionID, entry)
return nil
}
func (s *LocalCacheSessionAttributeStore) Invalidate(sessionID string) error {
s.rootStore.doInvalidateCacheCluster(s.rootStore.sessionAttributeCache, sessionID, nil)
return nil
}
func (s *LocalCacheSessionAttributeStore) Clear() error {
s.rootStore.doClearCacheCluster(s.rootStore.sessionAttributeCache)
return nil
}
func (s *LocalCacheSessionAttributeStore) Get(sessionID string) (map[string]any, map[string]int64, error) {
var entry *sessionAttributeEntry
if err := s.rootStore.doStandardReadCache(s.rootStore.sessionAttributeCache, sessionID, &entry); err != nil {
return nil, nil, err
}
if entry == nil {
return nil, nil, cache.ErrKeyNotFound
}
return entry.Attrs, entry.Timestamps, nil
}