Skip to content

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

Add cache to Pipeline

天舟52b99dbbcf82
diff --git a/api/cache.go b/api/cache.go
index c3ee4feefd..757bfc1717 100644
--- a/api/cache.go
+++ b/api/cache.go
@@ -9,6 +9,7 @@ const (
InstanceCache CacheNamespace = "i"
DatabaseCache CacheNamespace = "d"
MemberCache CacheNamespace = "m"
+ PipelineCache CacheNamespace = "pl"
)
type CacheService interface {
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go
index fb40700fe9..ffcb59ea79 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -253,7 +253,7 @@ func (m *main) Run() error {
server.InstanceService = store.NewInstanceService(m.l, db, server.CacheService, server.DatabaseService, server.DataSourceService)
server.TableService = store.NewTableService(m.l, db)
server.IssueService = store.NewIssueService(m.l, db)
- server.PipelineService = store.NewPipelineService(m.l, db)
+ server.PipelineService = store.NewPipelineService(m.l, db, server.CacheService)
server.StageService = store.NewStageService(m.l, db)
server.TaskService = store.NewTaskService(m.l, db, store.NewTaskRunService(m.l, db))
server.ActivityService = store.NewActivityService(m.l, db)
diff --git a/store/pipeline.go b/store/pipeline.go
index 371d74f2d3..84984a42a0 100644
--- a/store/pipeline.go
+++ b/store/pipeline.go
@@ -18,11 +18,13 @@ var (
type PipelineService struct {
l *zap.Logger
db *DB
+
+ cache api.CacheService
}
// NewPipelineService returns a new instance of PipelineService.
-func NewPipelineService(logger *zap.Logger, db *DB) *PipelineService {
- return &PipelineService{l: logger, db: db}
+func NewPipelineService(logger *zap.Logger, db *DB, cache api.CacheService) *PipelineService {
+ return &PipelineService{l: logger, db: db, cache: cache}
}
// CreatePipeline creates a new pipeline.
@@ -42,6 +44,10 @@ func (s *PipelineService) CreatePipeline(ctx context.Context, create *api.Pipeli
return nil, FormatError(err)
}
+ if err := s.cache.UpsertCache(api.PipelineCache, pipeline.ID, pipeline); err != nil {
+ return nil, err
+ }
+
return pipeline, nil
}
@@ -58,6 +64,14 @@ func (s *PipelineService) FindPipelineList(ctx context.Context, find *api.Pipeli
return []*api.Pipeline{}, err
}
+ if err == nil {
+ for _, pipeline := range list {
+ if err := s.cache.UpsertCache(api.PipelineCache, pipeline.ID, pipeline); err != nil {
+ return nil, err
+ }
+ }
+ }
+
return list, nil
}
@@ -65,6 +79,17 @@ func (s *PipelineService) FindPipelineList(ctx context.Context, find *api.Pipeli
// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *PipelineService) FindPipeline(ctx context.Context, find *api.PipelineFind) (*api.Pipeline, error) {
+ if find.ID != nil {
+ pipeline := &api.Pipeline{}
+ has, err := s.cache.FindCache(api.PipelineCache, *find.ID, pipeline)
+ if err != nil {
+ return nil, err
+ }
+ if has {
+ return pipeline, nil
+ }
+ }
+
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -79,6 +104,11 @@ func (s *PipelineService) FindPipeline(ctx context.Context, find *api.PipelineFi
} else if len(list) > 1 {
return nil, &bytebase.Error{Code: bytebase.ECONFLICT, Message: fmt.Sprintf("found %d pipelines with filter %+v, expect 1", len(list), find)}
}
+
+ if err := s.cache.UpsertCache(api.PipelineCache, list[0].ID, list[0]); err != nil {
+ return nil, err
+ }
+
return list[0], nil
}
@@ -100,6 +130,10 @@ func (s *PipelineService) PatchPipeline(ctx context.Context, patch *api.Pipeline
return nil, FormatError(err)
}
+ if err := s.cache.UpsertCache(api.PipelineCache, pipeline.ID, pipeline); err != nil {
+ return nil, err
+ }
+
return pipeline, nil
}