From d7a652ff7f865660a9108801e0c20c0868c6e35c Mon Sep 17 00:00:00 2001 From: Jo Date: Wed, 23 Nov 2022 11:04:38 +0000 Subject: [PATCH] User: Optimize signed in user cache management (#59090) * only access the cache if a user ID is set * ignore all negative values --- pkg/services/user/userimpl/user.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pkg/services/user/userimpl/user.go b/pkg/services/user/userimpl/user.go index f2250a5245d..132f556a1c2 100644 --- a/pkg/services/user/userimpl/user.go +++ b/pkg/services/user/userimpl/user.go @@ -232,11 +232,15 @@ func (s *Service) SetUsingOrg(ctx context.Context, cmd *user.SetUsingOrgCommand) func (s *Service) GetSignedInUserWithCacheCtx(ctx context.Context, query *user.GetSignedInUserQuery) (*user.SignedInUser, error) { var signedInUser *user.SignedInUser - cacheKey := newSignedInUserCacheKey(query.OrgID, query.UserID) - if cached, found := s.cacheService.Get(cacheKey); found { - cachedUser := cached.(user.SignedInUser) - signedInUser = &cachedUser - return signedInUser, nil + + // only check cache if we have a user ID and an org ID in query + if query.OrgID > 0 && query.UserID > 0 { + cacheKey := newSignedInUserCacheKey(query.OrgID, query.UserID) + if cached, found := s.cacheService.Get(cacheKey); found { + cachedUser := cached.(user.SignedInUser) + signedInUser = &cachedUser + return signedInUser, nil + } } result, err := s.GetSignedInUser(ctx, query) @@ -244,7 +248,7 @@ func (s *Service) GetSignedInUserWithCacheCtx(ctx context.Context, query *user.G return nil, err } - cacheKey = newSignedInUserCacheKey(result.OrgID, query.UserID) + cacheKey := newSignedInUserCacheKey(result.OrgID, result.UserID) s.cacheService.Set(cacheKey, *result, time.Second*5) return result, nil }