diff options
author | Corey Hulen <corey@hulen.com> | 2016-12-13 19:23:36 -0800 |
---|---|---|
committer | enahum <nahumhbl@gmail.com> | 2016-12-14 00:23:36 -0300 |
commit | aacbe995483b694e169acdc63136171dff3725d9 (patch) | |
tree | e60e67a1378ce929f42e41a49c2215a0289eae78 /store/sql_user_store.go | |
parent | 51b794501e21e1d19a58bb6dc273a5425c03a107 (diff) | |
download | chat-aacbe995483b694e169acdc63136171dff3725d9.tar.gz chat-aacbe995483b694e169acdc63136171dff3725d9.tar.bz2 chat-aacbe995483b694e169acdc63136171dff3725d9.zip |
PLT-4982 Adding caching to user profiles (#4782)
Diffstat (limited to 'store/sql_user_store.go')
-rw-r--r-- | store/sql_user_store.go | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/store/sql_user_store.go b/store/sql_user_store.go index 286b6551a..5882ed454 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -19,6 +19,8 @@ const ( MISSING_AUTH_ACCOUNT_ERROR = "store.sql_user.get_by_auth.missing_account.app_error" PROFILES_IN_CHANNEL_CACHE_SIZE = 5000 PROFILES_IN_CHANNEL_CACHE_SEC = 900 // 15 mins + PROFILE_BY_IDS_CACHE_SIZE = 20000 + PROFILE_BY_IDS_CACHE_SEC = 900 // 15 mins USER_SEARCH_OPTION_NAMES_ONLY = "names_only" USER_SEARCH_OPTION_NAMES_ONLY_NO_FULL_NAME = "names_only_no_full_name" USER_SEARCH_OPTION_ALL_NO_FULL_NAME = "all_no_full_name" @@ -34,9 +36,15 @@ type SqlUserStore struct { } var profilesInChannelCache *utils.Cache = utils.NewLru(PROFILES_IN_CHANNEL_CACHE_SIZE) +var profileByIdsCache *utils.Cache = utils.NewLru(PROFILE_BY_IDS_CACHE_SIZE) func ClearUserCaches() { profilesInChannelCache.Purge() + profileByIdsCache.Purge() +} + +func (us SqlUserStore) InvalidatProfileCacheForUser(userId string) { + profileByIdsCache.Remove(userId) } func NewSqlUserStore(sqlStore *SqlStore) UserStore { @@ -776,7 +784,7 @@ func (us SqlUserStore) GetRecentlyActiveUsersForTeam(teamId string) StoreChannel return storeChannel } -func (us SqlUserStore) GetProfileByIds(userIds []string) StoreChannel { +func (us SqlUserStore) GetProfileByIds(userIds []string, allowFromCache bool) StoreChannel { storeChannel := make(StoreChannel, 1) @@ -784,10 +792,33 @@ func (us SqlUserStore) GetProfileByIds(userIds []string) StoreChannel { result := StoreResult{} var users []*model.User + userMap := make(map[string]*model.User) props := make(map[string]interface{}) idQuery := "" + remainingUserIds := make([]string, 0) + + if allowFromCache { + for _, userId := range userIds { + if cacheItem, ok := profileByIdsCache.Get(userId); ok { + u := cacheItem.(*model.User) + userMap[u.Id] = u + } else { + remainingUserIds = append(remainingUserIds, userId) + } + } + } else { + remainingUserIds = userIds + } - for index, userId := range userIds { + // If everything came from the cache then just return + if len(remainingUserIds) == 0 { + result.Data = userMap + storeChannel <- result + close(storeChannel) + return + } + + for index, userId := range remainingUserIds { if len(idQuery) > 0 { idQuery += ", " } @@ -800,13 +831,12 @@ func (us SqlUserStore) GetProfileByIds(userIds []string) StoreChannel { result.Err = model.NewLocAppError("SqlUserStore.GetProfileByIds", "store.sql_user.get_profiles.app_error", nil, err.Error()) } else { - userMap := make(map[string]*model.User) - for _, u := range users { u.Password = "" u.AuthData = new(string) *u.AuthData = "" userMap[u.Id] = u + profileByIdsCache.AddWithExpiresInSecs(u.Id, u, PROFILE_BY_IDS_CACHE_SEC) } result.Data = userMap |