Skip to content

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

chore: add cache for database schema (#7520)

Danny Xu0200ab02476a
diff --git a/backend/enterprise/service/license.go b/backend/enterprise/service/license.go
index d422126c1a..69fde4f507 100644
--- a/backend/enterprise/service/license.go
+++ b/backend/enterprise/service/license.go
@@ -81,6 +81,7 @@ func (s *LicenseService) LoadSubscription(ctx context.Context) enterpriseAPI.Sub
license := s.loadLicense(ctx)
if license == nil {
+ s.store.RefreshSwap(false)
return enterpriseAPI.Subscription{
Plan: api.FREE,
// -1 means not expire, just for free plan
@@ -100,6 +101,9 @@ func (s *LicenseService) LoadSubscription(ctx context.Context) enterpriseAPI.Sub
OrgID: license.OrgID(),
OrgName: license.OrgName,
}
+ if !license.Trialing && license.Plan != api.FREE {
+ s.store.RefreshSwap(true)
+ }
return *s.cachedSubscription
}
diff --git a/backend/store/db_schema.go b/backend/store/db_schema.go
index 539d009dd8..f85c1effee 100644
--- a/backend/store/db_schema.go
+++ b/backend/store/db_schema.go
@@ -6,6 +6,7 @@ import (
"database/sql"
"fmt"
"strings"
+ "time"
"google.golang.org/protobuf/encoding/protojson"
@@ -139,8 +140,15 @@ func (s *DBSchema) FindIndex(schemaName string, tableName string, indexName stri
// GetDBSchema gets the schema for a database.
func (s *Store) GetDBSchema(ctx context.Context, databaseID int) (*DBSchema, error) {
- if dbSchema, ok := s.dbSchemaCache.Load(databaseID); ok {
- return dbSchema.(*DBSchema), nil
+ instanceCount := 0
+ s.instanceCache.Range(func(key, value any) bool {
+ instanceCount++
+ return true
+ })
+ if s.dbSchemaCache.MaxCost() != 1_000_000 || instanceCount <= 10 {
+ if dbSchema, ok := s.dbSchemaCache.Get(databaseID); ok {
+ return dbSchema.(*DBSchema), nil
+ }
}
// Build WHERE clause.
@@ -182,7 +190,7 @@ func (s *Store) GetDBSchema(ctx context.Context, databaseID int) (*DBSchema, err
}
dbSchema.Metadata = &databaseSchema
- s.dbSchemaCache.Store(databaseID, dbSchema)
+ s.dbSchemaCache.SetWithTTL(databaseID, dbSchema, int64(len(dbSchema.Schema)), 1*time.Hour)
return dbSchema, nil
}
@@ -227,6 +235,6 @@ func (s *Store) UpsertDBSchema(ctx context.Context, databaseID int, dbSchema *DB
return err
}
- s.dbSchemaCache.Store(databaseID, dbSchema)
+ s.dbSchemaCache.SetWithTTL(databaseID, dbSchema, int64(len(dbSchema.Schema)), 1*time.Hour)
return nil
}
diff --git a/backend/store/store.go b/backend/store/store.go
index 3b8bf74593..5d3de51785 100644
--- a/backend/store/store.go
+++ b/backend/store/store.go
@@ -16,29 +16,29 @@ 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
- risksCache sync.Map // []*RiskMessage, use 0 as the key
- databaseGroupCache sync.Map // map[string]*DatabaseGroupMessage
- databaseGroupIDCache sync.Map // map[int]*DatabaseGroupMessage
- schemaGroupCache sync.Map // map[string]*SchemaGroupMessage
+ 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 *ristretto.Cache // map[int]*DBSchema
+ settingCache sync.Map // map[string]*SettingMessage
+ idpCache sync.Map // map[string]*IdentityProvider
+ projectIDDeploymentConfigCache sync.Map // map[int]*DeploymentConfigMessage
+ risksCache sync.Map // []*RiskMessage, use 0 as the key
+ databaseGroupCache sync.Map // map[string]*DatabaseGroupMessage
+ databaseGroupIDCache sync.Map // map[int]*DatabaseGroupMessage
+ schemaGroupCache sync.Map // map[string]*SchemaGroupMessage
// sheetStatementCache caches the statement of a sheet.
sheetStatementCache *ristretto.Cache // map[sheetUID]sheetStatementString
vcsIDCache sync.Map // map[int]*ExternalVersionControlMessage
@@ -47,8 +47,16 @@ type Store struct {
// New creates a new instance of Store.
func New(db *DB) *Store {
sheetStatementCache, err := ristretto.NewCache(&ristretto.Config{
- NumCounters: 1000,
- MaxCost: 1 << 30, // 1 GB
+ NumCounters: 1_000,
+ MaxCost: 1_000_000_000, // ~1GB
+ BufferItems: 64,
+ })
+ if err != nil {
+ panic(fmt.Sprintf("failed to create sheet statement cache: %v", err))
+ }
+ dbsCache, err := ristretto.NewCache(&ristretto.Config{
+ NumCounters: 10_000,
+ MaxCost: 1_000_000, // ~1MB
BufferItems: 64,
})
if err != nil {
@@ -57,7 +65,17 @@ func New(db *DB) *Store {
return &Store{
db: db,
sheetStatementCache: sheetStatementCache,
+ dbSchemaCache: dbsCache,
+ }
+}
+
+// RefreshSwap updates the cache.
+func (s *Store) RefreshSwap(refresh bool) {
+ cost := int64(1_000_000)
+ if refresh {
+ cost = 500_000_000
}
+ s.dbSchemaCache.UpdateMaxCost(cost)
}
// Close closes underlying db.
diff --git a/frontend/src/components/ProjectWebhookForm.vue b/frontend/src/components/ProjectWebhookForm.vue
index 40b15f8aef..1cf8ee7d76 100644
--- a/frontend/src/components/ProjectWebhookForm.vue
+++ b/frontend/src/components/ProjectWebhookForm.vue
@@ -499,8 +499,6 @@ const deleteWebhook = () => {
const testWebhook = () => {
useGracefulRequest(async () => {
- console.log("Barny1", props.project);
- console.log("Barny2", state.webhook);
const result = await useProjectWebhookV1Store().testProjectWebhook(
props.project,
state.webhook