Skip to content

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

perf: add cache for workspace setting (#4530)

h3n4l3569a4d27be5
diff --git a/backend/store/setting.go b/backend/store/setting.go
index bfbf597a5f..eb14469814 100644
--- a/backend/store/setting.go
+++ b/backend/store/setting.go
@@ -14,11 +14,12 @@ import (
// CreateSettingIfNotExist creates an instance of Setting.
func (s *Store) CreateSettingIfNotExist(ctx context.Context, create *api.SettingCreate) (*api.Setting, bool, error) {
- settingRaw, created, err := s.createSettingRawIfNotExist(ctx, create)
+ setting, created, err := s.createSettingRawIfNotExist(ctx, create)
if err != nil {
return nil, false, errors.Wrapf(err, "failed to create Setting with SettingCreate[%+v]", create)
}
- return settingRaw.toAPISetting(), created, nil
+ s.settingCache.Store(setting.Name, setting)
+ return setting.toAPISetting(), created, nil
}
// FindSetting finds a list of Setting instances.
@@ -161,6 +162,11 @@ func (sm *SettingMessage) toAPISetting() *api.Setting {
// GetSettingV2 returns the setting by name.
func (s *Store) GetSettingV2(ctx context.Context, find *FindSettingMessage) (*SettingMessage, error) {
+ if find.Name != nil {
+ if setting, ok := s.settingCache.Load(*find.Name); ok {
+ return setting.(*SettingMessage), nil
+ }
+ }
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, errors.Wrap(err, "failed to begin transaction")
@@ -198,6 +204,9 @@ func (s *Store) ListSettingV2(ctx context.Context, find *FindSettingMessage) ([]
if err := tx.Commit(); err != nil {
return nil, errors.Wrap(err, "failed to commit transaction")
}
+ for _, setting := range settings {
+ s.settingCache.Store(setting.Name, setting)
+ }
return settings, nil
}
@@ -239,6 +248,7 @@ func (s *Store) UpsertSettingV2(ctx context.Context, update *SetSettingMessage,
if err := tx.Commit(); err != nil {
return nil, errors.Wrap(err, "failed to commit transaction")
}
+ s.settingCache.Store(setting.Name, &setting)
return &setting, nil
}
diff --git a/backend/store/store.go b/backend/store/store.go
index 6b3b2f7cc5..904810523c 100644
--- a/backend/store/store.go
+++ b/backend/store/store.go
@@ -29,6 +29,7 @@ type Store struct {
issueByPipelineCache sync.Map // map[int]*IssueMessage
pipelineCache sync.Map // map[int]*PipelineMessage
dbSchemaCache sync.Map // map[int]*DBSchema
+ settingCache sync.Map // map[string]*SettingMessage
}
// New creates a new instance of Store.