github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/store/localcachelayer/class_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 LocalCacheClassStore struct {
    12  	store.ClassStore
    13  	rootStore *LocalCacheStore
    14  }
    15  
    16  func (s *LocalCacheClassStore) handleClusterInvalidateClassMemberCounts(msg *model.ClusterMessage) {
    17  	if msg.Data == CLEAR_CACHE_MESSAGE_DATA {
    18  		s.rootStore.classMemberCountsCache.Purge()
    19  	} else {
    20  		s.rootStore.classMemberCountsCache.Remove(msg.Data)
    21  	}
    22  }
    23  
    24  func (s *LocalCacheClassStore) handleClusterInvalidateClassById(msg *model.ClusterMessage) {
    25  	if msg.Data == CLEAR_CACHE_MESSAGE_DATA {
    26  		s.rootStore.classByIdCache.Purge()
    27  	} else {
    28  		s.rootStore.classByIdCache.Remove(msg.Data)
    29  	}
    30  }
    31  
    32  func (s LocalCacheClassStore) ClearCaches() {
    33  	s.rootStore.doClearCacheCluster(s.rootStore.classMemberCountsCache)
    34  	s.rootStore.doClearCacheCluster(s.rootStore.classByIdCache)
    35  	s.ClassStore.ClearCaches()
    36  	if s.rootStore.metrics != nil {
    37  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Class Member Counts - Purge")
    38  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Class - Purge")
    39  	}
    40  }
    41  
    42  func (s LocalCacheClassStore) InvalidateMemberCount(classId string) {
    43  	s.rootStore.doInvalidateCacheCluster(s.rootStore.classMemberCountsCache, classId)
    44  	if s.rootStore.metrics != nil {
    45  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Class Member Counts - Remove by ClassId")
    46  	}
    47  }
    48  
    49  func (s LocalCacheClassStore) InvalidateClass(classId string) {
    50  	s.rootStore.doInvalidateCacheCluster(s.rootStore.classByIdCache, classId)
    51  	if s.rootStore.metrics != nil {
    52  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Class - Remove by ClassId")
    53  	}
    54  }
    55  
    56  func (s LocalCacheClassStore) GetMemberCount(classId string, allowFromCache bool) (int64, *model.AppError) {
    57  	if allowFromCache {
    58  		if count := s.rootStore.doStandardReadCache(s.rootStore.classMemberCountsCache, classId); count != nil {
    59  			return count.(int64), nil
    60  		}
    61  	}
    62  	count, err := s.ClassStore.GetMemberCount(classId, allowFromCache)
    63  
    64  	if allowFromCache && err == nil {
    65  		s.rootStore.doStandardAddToCache(s.rootStore.classMemberCountsCache, classId, count)
    66  	}
    67  
    68  	return count, err
    69  }
    70  
    71  func (s LocalCacheClassStore) GetMemberCountFromCache(classId string) int64 {
    72  	if count := s.rootStore.doStandardReadCache(s.rootStore.classMemberCountsCache, classId); count != nil {
    73  		return count.(int64)
    74  	}
    75  
    76  	count, err := s.GetMemberCount(classId, true)
    77  	if err != nil {
    78  		return 0
    79  	}
    80  
    81  	return count
    82  }
    83  
    84  func (s LocalCacheClassStore) Get(id string, allowFromCache bool) (*model.Class, *model.AppError) {
    85  
    86  	if allowFromCache {
    87  		if cacheItem := s.rootStore.doStandardReadCache(s.rootStore.classByIdCache, id); cacheItem != nil {
    88  			ch := cacheItem.(*model.Class).DeepCopy()
    89  			return ch, nil
    90  		}
    91  	}
    92  
    93  	ch, err := s.ClassStore.Get(id, allowFromCache)
    94  
    95  	if allowFromCache && err == nil {
    96  		s.rootStore.doStandardAddToCache(s.rootStore.classByIdCache, id, ch)
    97  	}
    98  
    99  	return ch, err
   100  }
   101  
   102  func (s LocalCacheClassStore) SaveMember(member *model.ClassMember) (*model.ClassMember, *model.AppError) {
   103  	member, err := s.ClassStore.SaveMember(member)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	s.InvalidateMemberCount(member.ClassId)
   108  	return member, nil
   109  }
   110  
   111  func (s LocalCacheClassStore) UpdateMember(member *model.ClassMember) (*model.ClassMember, *model.AppError) {
   112  	member, err := s.ClassStore.UpdateMember(member)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	s.InvalidateMemberCount(member.ClassId)
   117  	return member, nil
   118  }
   119  
   120  func (s LocalCacheClassStore) RemoveMember(classId, userId string) *model.AppError {
   121  	err := s.ClassStore.RemoveMember(classId, userId)
   122  	if err != nil {
   123  		return err
   124  	}
   125  	s.InvalidateMemberCount(classId)
   126  	return nil
   127  }