Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: add user email cache (#4286)

Danny Xud08b7fc96275
diff --git a/store/principal.go b/store/principal.go
index 4b52059978..8c5848e10c 100644
--- a/store/principal.go
+++ b/store/principal.go
@@ -114,6 +114,7 @@ func (s *Store) ListUsers(ctx context.Context, find *FindUserMessage) ([]*UserMe
for _, user := range users {
s.userIDCache.Store(user.ID, user)
+ s.userEmailCache.Store(user.Email, user)
}
return users, nil
}
@@ -146,6 +147,7 @@ func (s *Store) GetUserByID(ctx context.Context, id int) (*UserMessage, error) {
}
s.userIDCache.Store(user.ID, user)
+ s.userEmailCache.Store(user.Email, user)
return user, nil
}
@@ -154,6 +156,10 @@ func (s *Store) GetUserByEmail(ctx context.Context, email string) (*UserMessage,
// We use lower-case for emails.
email = strings.ToLower(email)
+ if user, ok := s.userEmailCache.Load(email); ok {
+ return user.(*UserMessage), nil
+ }
+
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -176,6 +182,7 @@ func (s *Store) GetUserByEmail(ctx context.Context, email string) (*UserMessage,
}
s.userIDCache.Store(user.ID, user)
+ s.userEmailCache.Store(user.Email, user)
return user, nil
}
@@ -323,6 +330,7 @@ func (s *Store) CreateUser(ctx context.Context, create *UserMessage, creatorID i
Role: role,
}
s.userIDCache.Store(user.ID, user)
+ s.userEmailCache.Store(user.Email, user)
return user, nil
}
@@ -331,6 +339,11 @@ func (s *Store) UpdateUser(ctx context.Context, userID int, patch *UpdateUserMes
if userID == api.SystemBotID {
return nil, errors.Errorf("cannot update system bot")
}
+ oldUser, err := s.GetUserByID(ctx, userID)
+ if err != nil {
+ return nil, err
+ }
+
principalSet, principalArgs := []string{"updater_id = $1"}, []interface{}{fmt.Sprintf("%d", updaterID)}
if v := patch.Email; v != nil {
principalSet, principalArgs = append(principalSet, fmt.Sprintf("email = $%d", len(principalArgs)+1)), append(principalArgs, *v)
@@ -404,7 +417,8 @@ func (s *Store) UpdateUser(ctx context.Context, userID int, patch *UpdateUserMes
if err := tx.Commit(); err != nil {
return nil, FormatError(err)
}
-
+ s.userEmailCache.Delete(oldUser.Email)
s.userIDCache.Store(user.ID, user)
+ s.userEmailCache.Store(user.Email, user)
return user, nil
}
diff --git a/store/store.go b/store/store.go
index 66df452dbb..4dfa398750 100644
--- a/store/store.go
+++ b/store/store.go
@@ -14,6 +14,7 @@ type Store struct {
cache *CacheService
userIDCache sync.Map // map[int]*UserMessage
+ userEmailCache sync.Map // map[string]*UserMessage
environmentCache sync.Map // map[string]*EnvironmentMessage
environmentIDCache sync.Map // map[int]*EnvironmentMessage
instanceCache sync.Map // map[string]*InstanceMessage