github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/store/localcachelayer/user_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  	"net/http"
     8  
     9  	"github.com/vnforks/kid/v5/model"
    10  	"github.com/vnforks/kid/v5/store"
    11  )
    12  
    13  type LocalCacheUserStore struct {
    14  	store.UserStore
    15  	rootStore *LocalCacheStore
    16  }
    17  
    18  func (s *LocalCacheUserStore) handleClusterInvalidateScheme(msg *model.ClusterMessage) {
    19  	if msg.Data == CLEAR_CACHE_MESSAGE_DATA {
    20  		s.rootStore.userProfileByIdsCache.Purge()
    21  	} else {
    22  		s.rootStore.userProfileByIdsCache.Remove(msg.Data)
    23  	}
    24  }
    25  
    26  func (s *LocalCacheUserStore) handleClusterInvalidateProfilesInClass(msg *model.ClusterMessage) {
    27  	if msg.Data == CLEAR_CACHE_MESSAGE_DATA {
    28  		s.rootStore.profilesInClassCache.Purge()
    29  	} else {
    30  		s.rootStore.profilesInClassCache.Remove(msg.Data)
    31  	}
    32  }
    33  
    34  func (s LocalCacheUserStore) ClearCaches() {
    35  	s.rootStore.userProfileByIdsCache.Purge()
    36  	s.rootStore.profilesInClassCache.Purge()
    37  
    38  	if s.rootStore.metrics != nil {
    39  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Profile By Ids - Purge")
    40  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Profiles in Class - Purge")
    41  	}
    42  }
    43  
    44  func (s LocalCacheUserStore) InvalidateProfileCacheForUser(userId string) {
    45  	s.rootStore.doInvalidateCacheCluster(s.rootStore.userProfileByIdsCache, userId)
    46  
    47  	if s.rootStore.metrics != nil {
    48  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Profile By Ids - Remove")
    49  	}
    50  }
    51  
    52  func (s LocalCacheUserStore) InvalidateProfilesInClassCacheByUser(userId string) {
    53  	keys := s.rootStore.profilesInClassCache.Keys()
    54  
    55  	for _, key := range keys {
    56  		if cacheItem, ok := s.rootStore.profilesInClassCache.Get(key); ok {
    57  			userMap := cacheItem.(map[string]*model.User)
    58  			if _, userInCache := userMap[userId]; userInCache {
    59  				s.rootStore.doInvalidateCacheCluster(s.rootStore.profilesInClassCache, key)
    60  				if s.rootStore.metrics != nil {
    61  					s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Profiles in Class - Remove by User")
    62  				}
    63  			}
    64  		}
    65  	}
    66  }
    67  
    68  func (s LocalCacheUserStore) InvalidateProfilesInClassCache(classId string) {
    69  	s.rootStore.doInvalidateCacheCluster(s.rootStore.profilesInClassCache, classId)
    70  	if s.rootStore.metrics != nil {
    71  		s.rootStore.metrics.IncrementMemCacheInvalidationCounter("Profiles in Class - Remove by Class")
    72  	}
    73  }
    74  
    75  func (s LocalCacheUserStore) GetAllProfilesInClass(classId string, allowFromCache bool) (map[string]*model.User, *model.AppError) {
    76  	if allowFromCache {
    77  		if cacheItem := s.rootStore.doStandardReadCache(s.rootStore.profilesInClassCache, classId); cacheItem != nil {
    78  			return cacheItem.(map[string]*model.User), nil
    79  		}
    80  	}
    81  
    82  	userMap, err := s.UserStore.GetAllProfilesInClass(classId, allowFromCache)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	if allowFromCache {
    88  		s.rootStore.doStandardAddToCache(s.rootStore.profilesInClassCache, classId, userMap)
    89  	}
    90  
    91  	return userMap, nil
    92  }
    93  
    94  func (s LocalCacheUserStore) GetProfileByIds(userIds []string, options *store.UserGetByIdsOpts, allowFromCache bool) ([]*model.User, *model.AppError) {
    95  	if !allowFromCache {
    96  		return s.UserStore.GetProfileByIds(userIds, options, false)
    97  	}
    98  
    99  	if options == nil {
   100  		options = &store.UserGetByIdsOpts{}
   101  	}
   102  
   103  	users := []*model.User{}
   104  	remainingUserIds := make([]string, 0)
   105  
   106  	for _, userId := range userIds {
   107  		if cacheItem := s.rootStore.doStandardReadCache(s.rootStore.userProfileByIdsCache, userId); cacheItem != nil {
   108  			u := cacheItem.(*model.User)
   109  
   110  			if options.Since == 0 || u.UpdateAt > options.Since {
   111  				users = append(users, u.DeepCopy())
   112  			}
   113  		} else {
   114  			remainingUserIds = append(remainingUserIds, userId)
   115  		}
   116  	}
   117  
   118  	if s.rootStore.metrics != nil {
   119  		s.rootStore.metrics.AddMemCacheHitCounter("Profile By Ids", float64(len(users)))
   120  		s.rootStore.metrics.AddMemCacheMissCounter("Profile By Ids", float64(len(remainingUserIds)))
   121  	}
   122  
   123  	if len(remainingUserIds) > 0 {
   124  		remainingUsers, err := s.UserStore.GetProfileByIds(remainingUserIds, options, false)
   125  		if err != nil {
   126  			return nil, model.NewAppError("SqlUserStore.GetProfileByIds", "store.sql_user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
   127  		}
   128  
   129  		for _, user := range remainingUsers {
   130  			users = append(users, user.DeepCopy())
   131  			s.rootStore.doStandardAddToCache(s.rootStore.userProfileByIdsCache, user.Id, user)
   132  		}
   133  
   134  	}
   135  
   136  	return users, nil
   137  }