FeatureToggle: for storing signed in user object in a Remote Cache (#59883)

* FeatureToggle: for storing sessions in a Remote Cache

Co-authored-by: ievaVasiljeva <ieva.vasiljeva@grafana.com>

* Fix conflicting modifications :D

Co-authored-by: ievaVasiljeva <ieva.vasiljeva@grafana.com>

* rename the flag to userRemoteCache

* undo unintended change

* Users: Add option to use remote cache for SignedInUsers (#59892)

* Add remote cache to GetSignedInUserWithCacheCtx

Co-authored-by: ievaVasiljeva <ieva.vasiljeva@grafana.com>

* Populate SignedInUser remote cache

Co-authored-by: ievaVasiljeva <ieva.vasiljeva@grafana.com>

* Line

* minor fixes to make this work

Co-authored-by: ievaVasiljeva <ieva.vasiljeva@grafana.com>

* Fix tests

* change flag to updated name

Co-authored-by: ievaVasiljeva <ieva.vasiljeva@grafana.com>

Co-authored-by: ievaVasiljeva <ieva.vasiljeva@grafana.com>
This commit is contained in:
Gabriel MABILLE
2022-12-07 08:58:15 +01:00
committed by GitHub
co-authored by ievaVasiljeva
parent 3c86bf550e
commit 91b15eed1a
7 changed files with 48 additions and 4 deletions
+28
View File
@@ -5,9 +5,11 @@ import (
"errors"
"time"
"github.com/grafana/grafana/pkg/infra/remotecache"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/org"
pref "github.com/grafana/grafana/pkg/services/preference"
"github.com/grafana/grafana/pkg/services/quota"
@@ -33,6 +35,8 @@ type Service struct {
userAuthService userauth.Service
quotaService quota.Service
accessControlStore accesscontrol.Service
remoteCache *remotecache.RemoteCache
features *featuremgmt.FeatureManager
// TODO remove sqlstore
sqlStore *sqlstore.SQLStore
@@ -51,7 +55,13 @@ func ProvideService(
accessControlStore accesscontrol.Service,
cfg *setting.Cfg,
ss *sqlstore.SQLStore,
remoteCache *remotecache.RemoteCache,
features *featuremgmt.FeatureManager,
) user.Service {
if features.IsEnabled(featuremgmt.FlagUserRemoteCache) {
remotecache.Register(user.SignedInUser{})
}
return &Service{
store: &sqlStore{
db: db,
@@ -67,6 +77,8 @@ func ProvideService(
accessControlStore: accessControlStore,
cfg: cfg,
sqlStore: ss,
remoteCache: remoteCache,
features: features,
}
}
@@ -309,6 +321,17 @@ func (s *Service) SetUsingOrg(ctx context.Context, cmd *user.SetUsingOrgCommand)
// TODO: remove wrapper around sqlstore
func (s *Service) GetSignedInUserWithCacheCtx(ctx context.Context, query *user.GetSignedInUserQuery) (*user.SignedInUser, error) {
// Fetching from remote cache first otherwise fallback to in memory cache
cacheKey := sqlstore.NewSignedInUserCacheKey(query.OrgID, query.UserID)
if s.features.IsEnabled(featuremgmt.FlagUserRemoteCache) {
res, errCache := s.remoteCache.Get(ctx, cacheKey)
if errCache == nil {
if signedInUser, ok := res.(user.SignedInUser); ok {
return &signedInUser, nil
}
}
}
q := &models.GetSignedInUserQuery{
UserId: query.UserID,
Login: query.Login,
@@ -319,6 +342,11 @@ func (s *Service) GetSignedInUserWithCacheCtx(ctx context.Context, query *user.G
if err != nil {
return nil, err
}
// Remember user in remote cache
if s.features.IsEnabled(featuremgmt.FlagUserRemoteCache) {
_ = s.remoteCache.Set(ctx, cacheKey, *(q.Result), time.Second*5)
}
return q.Result, nil
}