github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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/vnforks/kid/v5/model"
     8  	"github.com/vnforks/kid/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 == CLEAR_CACHE_MESSAGE_DATA {
    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, *model.AppError) {
    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, *model.AppError) {
    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, *model.AppError) {
    35  	if !allowFromCache {
    36  		return s.ReactionStore.GetForPost(postId, false)
    37  	}
    38  
    39  	if reaction := s.rootStore.doStandardReadCache(s.rootStore.reactionCache, postId); reaction != nil {
    40  		return reaction.([]*model.Reaction), nil
    41  	}
    42  
    43  	reaction, err := s.ReactionStore.GetForPost(postId, false)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	s.rootStore.doStandardAddToCache(s.rootStore.reactionCache, postId, reaction)
    49  
    50  	return reaction, nil
    51  }
    52  
    53  func (s LocalCacheReactionStore) DeleteAllWithEmojiName(emojiName string) *model.AppError {
    54  	// This could be improved. Right now we just clear the whole
    55  	// cache because we don't have a way find what post Ids have this emoji name.
    56  	defer s.rootStore.doClearCacheCluster(s.rootStore.reactionCache)
    57  	return s.ReactionStore.DeleteAllWithEmojiName(emojiName)
    58  }