github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/store/localcachelayer/reaction_layer.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package localcachelayer
     5  
     6  import (
     7  	"github.com/mattermost/mattermost-server/v5/model"
     8  	"github.com/mattermost/mattermost-server/v5/store"
     9  )
    10  
    11  type LocalCacheReactionStore struct {
    12  	store.ReactionStore
    13  	rootStore *LocalCacheStore
    14  }
    15  
    16  func (s *LocalCacheReactionStore) handleClusterInvalidateReaction(msg *model.ClusterMessage) {
    17  	if msg.Data == ClearCacheMessageData {
    18  		s.rootStore.reactionCache.Purge()
    19  	} else {
    20  		s.rootStore.reactionCache.Remove(msg.Data)
    21  	}
    22  }
    23  
    24  func (s LocalCacheReactionStore) Save(reaction *model.Reaction) (*model.Reaction, error) {
    25  	defer s.rootStore.doInvalidateCacheCluster(s.rootStore.reactionCache, reaction.PostId)
    26  	return s.ReactionStore.Save(reaction)
    27  }
    28  
    29  func (s LocalCacheReactionStore) Delete(reaction *model.Reaction) (*model.Reaction, error) {
    30  	defer s.rootStore.doInvalidateCacheCluster(s.rootStore.reactionCache, reaction.PostId)
    31  	return s.ReactionStore.Delete(reaction)
    32  }
    33  
    34  func (s LocalCacheReactionStore) GetForPost(postId string, allowFromCache bool) ([]*model.Reaction, error) {
    35  	if !allowFromCache {
    36  		return s.ReactionStore.GetForPost(postId, false)
    37  	}
    38  
    39  	var reaction []*model.Reaction
    40  	if err := s.rootStore.doStandardReadCache(s.rootStore.reactionCache, postId, &reaction); err == nil {
    41  		return reaction, nil
    42  	}
    43  
    44  	reaction, err := s.ReactionStore.GetForPost(postId, false)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	s.rootStore.doStandardAddToCache(s.rootStore.reactionCache, postId, reaction)
    50  
    51  	return reaction, nil
    52  }
    53  
    54  func (s LocalCacheReactionStore) DeleteAllWithEmojiName(emojiName string) error {
    55  	// This could be improved. Right now we just clear the whole
    56  	// cache because we don't have a way find what post Ids have this emoji name.
    57  	defer s.rootStore.doClearCacheCluster(s.rootStore.reactionCache)
    58  	return s.ReactionStore.DeleteAllWithEmojiName(emojiName)
    59  }