Skip to content

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

cache risk list (#5271)

p0ny8fd11a20384d
diff --git a/backend/store/risk.go b/backend/store/risk.go
index ff2dee3cc8..4efafeecf0 100644
--- a/backend/store/risk.go
+++ b/backend/store/risk.go
@@ -101,6 +101,12 @@ func (s *Store) GetRisk(ctx context.Context, id int64) (*RiskMessage, error) {
// ListRisks lists risks.
func (s *Store) ListRisks(ctx context.Context) ([]*RiskMessage, error) {
+ if cached := s.risksCache.Load(); cached != nil {
+ if risks, ok := cached.([]*RiskMessage); ok {
+ return risks, nil
+ }
+ }
+
query := `
SELECT
id,
@@ -157,6 +163,7 @@ func (s *Store) ListRisks(ctx context.Context) ([]*RiskMessage, error) {
return nil, errors.Wrap(err, "failed to commit")
}
+ s.risksCache.Store(risks)
return risks, nil
}
@@ -194,6 +201,7 @@ func (s *Store) CreateRisk(ctx context.Context, risk *RiskMessage, creatorID int
return nil, errors.Wrap(err, "failed to commit")
}
+ s.risksCache.Store(([]*RiskMessage)(nil))
return &RiskMessage{
ID: id,
Source: risk.Source,
@@ -247,5 +255,6 @@ func (s *Store) UpdateRisk(ctx context.Context, patch *UpdateRiskMessage, id int
return nil, errors.Wrap(err, "failed to commit")
}
+ s.risksCache.Store(([]*RiskMessage)(nil))
return s.GetRisk(ctx, id)
}
diff --git a/backend/store/store.go b/backend/store/store.go
index 2769193193..599ecb4cc3 100644
--- a/backend/store/store.go
+++ b/backend/store/store.go
@@ -4,6 +4,7 @@ package store
import (
"fmt"
"sync"
+ "sync/atomic"
api "github.com/bytebase/bytebase/backend/legacyapi"
)
@@ -12,25 +13,26 @@ import (
type Store struct {
db *DB
- userIDCache sync.Map // map[int]*UserMessage
- environmentCache sync.Map // map[string]*EnvironmentMessage
- environmentIDCache sync.Map // map[int]*EnvironmentMessage
- instanceCache sync.Map // map[string]*InstanceMessage
- instanceIDCache sync.Map // map[int]*InstanceMessage
- databaseCache sync.Map // map[string]*DatabaseMessage
- databaseIDCache sync.Map // map[int]*DatabaseMessage
- projectCache sync.Map // map[string]*ProjectMessage
- projectIDCache sync.Map // map[int]*ProjectMessage
- projectPolicyCache sync.Map // map[string]*IAMPolicyMessage
- projectIDPolicyCache sync.Map // map[int]*IAMPolicyMessage
- policyCache sync.Map // map[string]*PolicyMessage
- issueCache sync.Map // map[int]*IssueMessage
- issueByPipelineCache sync.Map // map[int]*IssueMessage
- pipelineCache sync.Map // map[int]*PipelineMessage
- dbSchemaCache sync.Map // map[int]*DBSchema
- settingCache sync.Map // map[string]*SettingMessage
- idpCache sync.Map // map[string]*IdentityProvider
- projectIDDeploymentConfigCache sync.Map // map[int]*DeploymentConfigMessage
+ userIDCache sync.Map // map[int]*UserMessage
+ environmentCache sync.Map // map[string]*EnvironmentMessage
+ environmentIDCache sync.Map // map[int]*EnvironmentMessage
+ instanceCache sync.Map // map[string]*InstanceMessage
+ instanceIDCache sync.Map // map[int]*InstanceMessage
+ databaseCache sync.Map // map[string]*DatabaseMessage
+ databaseIDCache sync.Map // map[int]*DatabaseMessage
+ projectCache sync.Map // map[string]*ProjectMessage
+ projectIDCache sync.Map // map[int]*ProjectMessage
+ projectPolicyCache sync.Map // map[string]*IAMPolicyMessage
+ projectIDPolicyCache sync.Map // map[int]*IAMPolicyMessage
+ policyCache sync.Map // map[string]*PolicyMessage
+ issueCache sync.Map // map[int]*IssueMessage
+ issueByPipelineCache sync.Map // map[int]*IssueMessage
+ pipelineCache sync.Map // map[int]*PipelineMessage
+ dbSchemaCache sync.Map // map[int]*DBSchema
+ settingCache sync.Map // map[string]*SettingMessage
+ idpCache sync.Map // map[string]*IdentityProvider
+ projectIDDeploymentConfigCache sync.Map // map[int]*DeploymentConfigMessage
+ risksCache atomic.Value // []*RiskMessage
}
// New creates a new instance of Store.