Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: move cache to store closing to where it is used (#3718)
Danny Xuf674e5ce5f8c
diff --git a/api/cache.go b/api/cache.godeleted file mode 100644index 455b59c601..0000000000--- a/api/cache.go+++ /dev/null@@ -1,38 +0,0 @@-package api--// CacheNamespace is the type of a cache.-type CacheNamespace string--const (- // PrincipalCache is the cache type of principals.- PrincipalCache CacheNamespace = "p"- // EnvironmentCache is the cache type of environments.- EnvironmentCache CacheNamespace = "e"- // ProjectCache is the cache type of projects.- ProjectCache CacheNamespace = "r"- // ProjectMemberCache is the cache type of project members.- ProjectMemberCache CacheNamespace = "pm"- // InstanceCache is the cache type of instances.- InstanceCache CacheNamespace = "i"- // DatabaseCache is the cache type of databases.- DatabaseCache CacheNamespace = "d"- // MemberCache is the cache type of members.- MemberCache CacheNamespace = "m"- // PipelineCache is the cache type of pipelines.- PipelineCache CacheNamespace = "pl"- // IssueCache is the cache type of issues.- IssueCache CacheNamespace = "is"- // DatabaseLabelCache is the cache type of database labels.- DatabaseLabelCache CacheNamespace = "dl"- // DataSourceCache is the cache type of data sources.- DataSourceCache CacheNamespace = "ds"- // TierPolicyCache is the cache type of tier policy.- TierPolicyCache CacheNamespace = "pot"-)--// CacheService is the service for caches.-type CacheService interface {- FindCache(namespace CacheNamespace, id int, entry interface{}) (bool, error)- UpsertCache(namespace CacheNamespace, id int, entry interface{}) error- DeleteCache(namespace CacheNamespace, id int)-}diff --git a/server/cache.go b/server/cache.godeleted file mode 100644index b88e7c5e2d..0000000000--- a/server/cache.go+++ /dev/null@@ -1,69 +0,0 @@-package server--import (- "bytes"- "encoding/binary"- "encoding/gob"-- "github.com/VictoriaMetrics/fastcache"- "github.com/pkg/errors"-- "github.com/bytebase/bytebase/api"-)--var (- // 32 MiB.- cacheSize = 1024 * 1024 * 32- _ api.CacheService = (*CacheService)(nil)-)--// CacheService implements a cache.-type CacheService struct {- cache *fastcache.Cache-}--// NewCacheService creates a cache service.-func NewCacheService() *CacheService {- return &CacheService{- cache: fastcache.New(cacheSize),- }-}--// FindCache finds the value in cache.-func (s *CacheService) FindCache(namespace api.CacheNamespace, id int, entry interface{}) (bool, error) {- buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}- binary.LittleEndian.PutUint64(buf1, uint64(id))-- buf2, has := s.cache.HasGet(nil, append([]byte(namespace), buf1...))- if has {- dec := gob.NewDecoder(bytes.NewReader(buf2))- if err := dec.Decode(entry); err != nil {- return false, errors.Wrapf(err, "failed to decode entry for cache namespace: %s", namespace)- }- return true, nil- }-- return false, nil-}--// UpsertCache upserts the value to cache.-func (s *CacheService) UpsertCache(namespace api.CacheNamespace, id int, entry interface{}) error {- buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}- binary.LittleEndian.PutUint64(buf1, uint64(id))-- var buf2 bytes.Buffer- enc := gob.NewEncoder(&buf2)- if err := enc.Encode(entry); err != nil {- return errors.Wrapf(err, "failed to encode entry for cache namespace: %s", namespace)- }- s.cache.Set(append([]byte(namespace), buf1...), buf2.Bytes())-- return nil-}--// DeleteCache deletes the key from cache.-func (s *CacheService) DeleteCache(namespace api.CacheNamespace, id int) {- buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}- binary.LittleEndian.PutUint64(buf1, uint64(id))- s.cache.Del(append([]byte(namespace), buf1...))-}diff --git a/server/server.go b/server/server.goindex 52299353cb..ee16619a61 100644--- a/server/server.go+++ b/server/server.go@@ -232,8 +232,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {return nil, errors.Wrap(err, "cannot open db")}- cacheService := NewCacheService()- storeInstance := store.New(storeDB, cacheService)+ storeInstance := store.New(storeDB)s.store = storeInstance// TODO(d): backfill activity. Remove this whenever the backfill is completed over the time.if err := storeInstance.BackfillSQLEditorActivity(ctx); err != nil {diff --git a/store/cache.go b/store/cache.gonew file mode 100644index 0000000000..9664bbd83f--- /dev/null+++ b/store/cache.go@@ -0,0 +1,96 @@+package store++import (+ "bytes"+ "encoding/binary"+ "encoding/gob"++ "github.com/VictoriaMetrics/fastcache"+ "github.com/pkg/errors"+)++var (+ // 32 MiB.+ cacheSize = 1024 * 1024 * 32+)++// cacheNamespace is the type of a cache.+type cacheNamespace string++const (+ // principalCacheNamespace is the cache type of principals.+ principalCacheNamespace cacheNamespace = "p"+ // environmentCacheNamespace is the cache type of environments.+ environmentCacheNamespace cacheNamespace = "e"+ // projectCacheNamespace is the cache type of projects.+ projectCacheNamespace cacheNamespace = "r"+ // projectMemberCacheNamespace is the cache type of project members.+ projectMemberCacheNamespace cacheNamespace = "pm"+ // instanceCacheNamespace is the cache type of instances.+ instanceCacheNamespace cacheNamespace = "i"+ // databaseCacheNamespace is the cache type of databases.+ databaseCacheNamespace cacheNamespace = "d"+ // memberCacheNamespace is the cache type of members.+ memberCacheNamespace cacheNamespace = "m"+ // pipelineCacheNamespace is the cache type of pipelines.+ pipelineCacheNamespace cacheNamespace = "pl"+ // issueCacheNamespace is the cache type of issues.+ issueCacheNamespace cacheNamespace = "is"+ // databaseLabelCacheNamespace is the cache type of database labels.+ databaseLabelCacheNamespace cacheNamespace = "dl"+ // dataSourceCacheNamespace is the cache type of data sources.+ dataSourceCacheNamespace cacheNamespace = "ds"+ // tierPolicyCacheNamespace is the cache type of tier policy.+ tierPolicyCacheNamespace cacheNamespace = "pot"+)++// CacheService implements a cache.+type CacheService struct {+ cache *fastcache.Cache+}++// newCacheService creates a cache service.+func newCacheService() *CacheService {+ return &CacheService{+ cache: fastcache.New(cacheSize),+ }+}++// FindCache finds the value in cache.+func (s *CacheService) FindCache(namespace cacheNamespace, id int, entry interface{}) (bool, error) {+ buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}+ binary.LittleEndian.PutUint64(buf1, uint64(id))++ buf2, has := s.cache.HasGet(nil, append([]byte(namespace), buf1...))+ if has {+ dec := gob.NewDecoder(bytes.NewReader(buf2))+ if err := dec.Decode(entry); err != nil {+ return false, errors.Wrapf(err, "failed to decode entry for cache namespace: %s", namespace)+ }+ return true, nil+ }++ return false, nil+}++// UpsertCache upserts the value to cache.+func (s *CacheService) UpsertCache(namespace cacheNamespace, id int, entry interface{}) error {+ buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}+ binary.LittleEndian.PutUint64(buf1, uint64(id))++ var buf2 bytes.Buffer+ enc := gob.NewEncoder(&buf2)+ if err := enc.Encode(entry); err != nil {+ return errors.Wrapf(err, "failed to encode entry for cache namespace: %s", namespace)+ }+ s.cache.Set(append([]byte(namespace), buf1...), buf2.Bytes())++ return nil+}++// DeleteCache deletes the key from cache.+func (s *CacheService) DeleteCache(namespace cacheNamespace, id int) {+ buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}+ binary.LittleEndian.PutUint64(buf1, uint64(id))+ s.cache.Del(append([]byte(namespace), buf1...))+}diff --git a/store/data_source.go b/store/data_source.goindex 689b96a7a9..f528b4c8e5 100644--- a/store/data_source.go+++ b/store/data_source.go@@ -145,7 +145,7 @@ func (s *Store) DeleteDataSource(ctx context.Context, deleteDataSource *api.Data}// Invalidate the cache.- s.cache.DeleteCache(api.DataSourceCache, deleteDataSource.InstanceID)+ s.cache.DeleteCache(dataSourceCacheNamespace, deleteDataSource.InstanceID)return nil}@@ -160,7 +160,7 @@ func (s *Store) createDataSourceRawTx(ctx context.Context, tx *Tx, create *api.Dreturn errors.Wrapf(err, "failed to create data source with DataSourceCreate[%+v]", create)}// Invalidate the cache.- s.cache.DeleteCache(api.DataSourceCache, create.InstanceID)+ s.cache.DeleteCache(dataSourceCacheNamespace, create.InstanceID)return nil}@@ -199,7 +199,7 @@ func (s *Store) createDataSourceRaw(ctx context.Context, create *api.DataSourceCreturn nil, FormatError(err)}// Invalidate the cache.- s.cache.DeleteCache(api.DataSourceCache, dataSource.InstanceID)+ s.cache.DeleteCache(dataSourceCacheNamespace, dataSource.InstanceID)return dataSource, nil}@@ -210,7 +210,7 @@ func (s *Store) findDataSourceRaw(ctx context.Context, find *api.DataSourceFind)findCopy.InstanceID = nilisListDataSource := find.InstanceID != nil && findCopy == api.DataSourceFind{}var cacheList []*dataSourceRaw- has, err := s.cache.FindCache(api.DataSourceCache, *find.InstanceID, &cacheList)+ has, err := s.cache.FindCache(dataSourceCacheNamespace, *find.InstanceID, &cacheList)if err != nil {return nil, err}@@ -229,7 +229,7 @@ func (s *Store) findDataSourceRaw(ctx context.Context, find *api.DataSourceFind)return nil, err}if isListDataSource {- if err := s.cache.UpsertCache(api.DataSourceCache, *find.InstanceID, list); err != nil {+ if err := s.cache.UpsertCache(dataSourceCacheNamespace, *find.InstanceID, list); err != nil {return nil, err}}@@ -276,7 +276,7 @@ func (s *Store) patchDataSourceRaw(ctx context.Context, patch *api.DataSourcePatreturn nil, FormatError(err)}// Invalidate the cache.- s.cache.DeleteCache(api.DataSourceCache, dataSource.InstanceID)+ s.cache.DeleteCache(dataSourceCacheNamespace, dataSource.InstanceID)return dataSource, nil}diff --git a/store/database.go b/store/database.goindex 52b2f10273..7e1704c352 100644--- a/store/database.go+++ b/store/database.go@@ -329,7 +329,7 @@ func (s *Store) createDatabaseRawTx(ctx context.Context, tx *Tx, create *api.Dat}}- if err := s.cache.UpsertCache(api.DatabaseCache, databaseRaw.ID, databaseRaw); err != nil {+ if err := s.cache.UpsertCache(databaseCacheNamespace, databaseRaw.ID, databaseRaw); err != nil {return nil, err}@@ -351,7 +351,7 @@ func (s *Store) findDatabaseRaw(ctx context.Context, find *api.DatabaseFind) ([]if err == nil {for _, database := range list {- if err := s.cache.UpsertCache(api.DatabaseCache, database.ID, database); err != nil {+ if err := s.cache.UpsertCache(databaseCacheNamespace, database.ID, database); err != nil {return nil, err}}@@ -365,7 +365,7 @@ func (s *Store) findDatabaseRaw(ctx context.Context, find *api.DatabaseFind) ([]func (s *Store) getDatabaseRaw(ctx context.Context, find *api.DatabaseFind) (*databaseRaw, error) {if find.ID != nil {databaseRaw := &databaseRaw{}- has, err := s.cache.FindCache(api.DatabaseCache, *find.ID, databaseRaw)+ has, err := s.cache.FindCache(databaseCacheNamespace, *find.ID, databaseRaw)if err != nil {return nil, err}@@ -391,7 +391,7 @@ func (s *Store) getDatabaseRaw(ctx context.Context, find *api.DatabaseFind) (*dareturn nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d databases with filter %+v, expect 1. ", len(list), find)}}- if err := s.cache.UpsertCache(api.DatabaseCache, list[0].ID, list[0]); err != nil {+ if err := s.cache.UpsertCache(databaseCacheNamespace, list[0].ID, list[0]); err != nil {return nil, err}@@ -416,7 +416,7 @@ func (s *Store) patchDatabaseRaw(ctx context.Context, patch *api.DatabasePatch)return nil, FormatError(err)}- if err := s.cache.UpsertCache(api.DatabaseCache, database.ID, database); err != nil {+ if err := s.cache.UpsertCache(databaseCacheNamespace, database.ID, database); err != nil {return nil, err}diff --git a/store/environment.go b/store/environment.goindex 7b3523273c..8c12b0cdfe 100644--- a/store/environment.go+++ b/store/environment.go@@ -156,7 +156,7 @@ func (s *Store) createEnvironmentRaw(ctx context.Context, create *api.Environmenreturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.EnvironmentCache, environment.ID, environment); err != nil {+ if err := s.cache.UpsertCache(environmentCacheNamespace, environment.ID, environment); err != nil {return nil, err}@@ -178,7 +178,7 @@ func (s *Store) findEnvironmentRaw(ctx context.Context, find *api.EnvironmentFinif err == nil {for _, environment := range list {- if err := s.cache.UpsertCache(api.EnvironmentCache, environment.ID, environment); err != nil {+ if err := s.cache.UpsertCache(environmentCacheNamespace, environment.ID, environment); err != nil {return nil, err}}@@ -191,7 +191,7 @@ func (s *Store) findEnvironmentRaw(ctx context.Context, find *api.EnvironmentFin// Returns ECONFLICT if finding more than 1 matching records.func (s *Store) getEnvironmentByIDRaw(ctx context.Context, id int) (*environmentRaw, error) {envRaw := &environmentRaw{}- has, err := s.cache.FindCache(api.EnvironmentCache, id, envRaw)+ has, err := s.cache.FindCache(environmentCacheNamespace, id, envRaw)if err != nil {return nil, err}@@ -216,7 +216,7 @@ func (s *Store) getEnvironmentByIDRaw(ctx context.Context, id int) (*environment} else if len(envRawList) > 1 {return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d environments with filter %+v, expect 1", len(envRawList), find)}}- if err := s.cache.UpsertCache(api.EnvironmentCache, envRawList[0].ID, envRawList[0]); err != nil {+ if err := s.cache.UpsertCache(environmentCacheNamespace, envRawList[0].ID, envRawList[0]); err != nil {return nil, err}return envRawList[0], nil@@ -240,7 +240,7 @@ func (s *Store) patchEnvironmentRaw(ctx context.Context, patch *api.EnvironmentPreturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.EnvironmentCache, envRaw.ID, envRaw); err != nil {+ if err := s.cache.UpsertCache(environmentCacheNamespace, envRaw.ID, envRaw); err != nil {return nil, err}diff --git a/store/instance.go b/store/instance.goindex 4a5ade5318..5408b076e9 100644--- a/store/instance.go+++ b/store/instance.go@@ -380,7 +380,7 @@ func (s *Store) createInstanceRaw(ctx context.Context, create *api.InstanceCreatreturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.InstanceCache, instance.ID, instance); err != nil {+ if err := s.cache.UpsertCache(instanceCacheNamespace, instance.ID, instance); err != nil {return nil, err}@@ -408,7 +408,7 @@ func (s *Store) findInstanceRaw(ctx context.Context, find *api.InstanceFind) ([]func (s *Store) getInstanceRaw(ctx context.Context, find *api.InstanceFind) (*instanceRaw, error) {if find.ID != nil {instanceRaw := &instanceRaw{}- has, err := s.cache.FindCache(api.InstanceCache, *find.ID, instanceRaw)+ has, err := s.cache.FindCache(instanceCacheNamespace, *find.ID, instanceRaw)if err != nil {return nil, err}@@ -435,7 +435,7 @@ func (s *Store) getInstanceRaw(ctx context.Context, find *api.InstanceFind) (*in}instance := list[0]- if err := s.cache.UpsertCache(api.InstanceCache, instance.ID, instance); err != nil {+ if err := s.cache.UpsertCache(instanceCacheNamespace, instance.ID, instance); err != nil {return nil, err}return instance, nil@@ -459,7 +459,7 @@ func (s *Store) patchInstanceRaw(ctx context.Context, patch *api.InstancePatch)return nil, FormatError(err)}- if err := s.cache.UpsertCache(api.InstanceCache, instance.ID, instance); err != nil {+ if err := s.cache.UpsertCache(instanceCacheNamespace, instance.ID, instance); err != nil {return nil, err}diff --git a/store/issue.go b/store/issue.goindex ee5c9066ce..503c68abdd 100644--- a/store/issue.go+++ b/store/issue.go@@ -495,7 +495,7 @@ func (s *Store) createIssueRaw(ctx context.Context, create *api.IssueCreate) (*ireturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.IssueCache, issue.ID, issue); err != nil {+ if err := s.cache.UpsertCache(issueCacheNamespace, issue.ID, issue); err != nil {return nil, err}@@ -517,7 +517,7 @@ func (s *Store) findIssueRaw(ctx context.Context, find *api.IssueFind) ([]*issueif err == nil {for _, issue := range list {- if err := s.cache.UpsertCache(api.IssueCache, issue.ID, issue); err != nil {+ if err := s.cache.UpsertCache(issueCacheNamespace, issue.ID, issue); err != nil {return nil, err}}@@ -545,7 +545,7 @@ func (s *Store) getIssueRaw(ctx context.Context, find *api.IssueFind) (*issueRaw} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d issues with filter %+v, expect 1", len(list), find)}}- if err := s.cache.UpsertCache(api.IssueCache, list[0].ID, list[0]); err != nil {+ if err := s.cache.UpsertCache(issueCacheNamespace, list[0].ID, list[0]); err != nil {return nil, err}return list[0], nil@@ -569,7 +569,7 @@ func (s *Store) patchIssueRaw(ctx context.Context, patch *api.IssuePatch) (*issureturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.IssueCache, issue.ID, issue); err != nil {+ if err := s.cache.UpsertCache(issueCacheNamespace, issue.ID, issue); err != nil {return nil, err}diff --git a/store/label.go b/store/label.goindex f23dfd8b57..2921d135ba 100644--- a/store/label.go+++ b/store/label.go@@ -127,7 +127,7 @@ func (s *Store) SetDatabaseLabelList(ctx context.Context, labelList []*api.Databreturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.DatabaseLabelCache, databaseID, ret); err != nil {+ if err := s.cache.UpsertCache(databaseLabelCacheNamespace, databaseID, ret); err != nil {return nil, err}@@ -167,7 +167,7 @@ func (s *Store) PatchLabelKey(ctx context.Context, patch *api.LabelKeyPatch) (*a// findDatabaseLabel finds a list of DatabaseLabel instances.func (s *Store) findDatabaseLabel(ctx context.Context, find *api.DatabaseLabelFind) ([]*api.DatabaseLabel, error) {var l []*api.DatabaseLabel- has, err := s.cache.FindCache(api.DatabaseLabelCache, find.DatabaseID, &l)+ has, err := s.cache.FindCache(databaseLabelCacheNamespace, find.DatabaseID, &l)if err != nil {return nil, err}@@ -182,7 +182,7 @@ func (s *Store) findDatabaseLabel(ctx context.Context, find *api.DatabaseLabelFifor _, raw := range labelKeyRawList {l = append(l, &api.DatabaseLabel{Key: raw.Key, Value: raw.Value})}- if err := s.cache.UpsertCache(api.DatabaseLabelCache, find.DatabaseID, l); err != nil {+ if err := s.cache.UpsertCache(databaseLabelCacheNamespace, find.DatabaseID, l); err != nil {return nil, err}return l, nildiff --git a/store/member.go b/store/member.goindex 604df3b33d..6f69360f4f 100644--- a/store/member.go+++ b/store/member.go@@ -182,7 +182,7 @@ func (s *Store) createMemberRaw(ctx context.Context, create *api.MemberCreate) (return nil, FormatError(err)}- if err := s.cache.UpsertCache(api.MemberCache, member.PrincipalID, member); err != nil {+ if err := s.cache.UpsertCache(memberCacheNamespace, member.PrincipalID, member); err != nil {return nil, err}@@ -203,7 +203,7 @@ func (s *Store) findMemberRaw(ctx context.Context, find *api.MemberFind) ([]*mem}for _, member := range memberRawList {- if err := s.cache.UpsertCache(api.MemberCache, member.PrincipalID, member); err != nil {+ if err := s.cache.UpsertCache(memberCacheNamespace, member.PrincipalID, member); err != nil {return nil, err}}@@ -216,7 +216,7 @@ func (s *Store) findMemberRaw(ctx context.Context, find *api.MemberFind) ([]*memfunc (s *Store) getMemberRaw(ctx context.Context, find *api.MemberFind) (*memberRaw, error) {if find.PrincipalID != nil {memberRaw := &memberRaw{}- has, err := s.cache.FindCache(api.MemberCache, *find.PrincipalID, memberRaw)+ has, err := s.cache.FindCache(memberCacheNamespace, *find.PrincipalID, memberRaw)if err != nil {return nil, err}@@ -241,7 +241,7 @@ func (s *Store) getMemberRaw(ctx context.Context, find *api.MemberFind) (*member} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d members with filter %+v, expect 1", len(list), find)}}- if err := s.cache.UpsertCache(api.MemberCache, list[0].PrincipalID, list[0]); err != nil {+ if err := s.cache.UpsertCache(memberCacheNamespace, list[0].PrincipalID, list[0]); err != nil {return nil, err}return list[0], nil@@ -265,7 +265,7 @@ func (s *Store) patchMemberRaw(ctx context.Context, patch *api.MemberPatch) (*mereturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.MemberCache, member.PrincipalID, member); err != nil {+ if err := s.cache.UpsertCache(memberCacheNamespace, member.PrincipalID, member); err != nil {return nil, err}diff --git a/store/pipeline.go b/store/pipeline.goindex 4a529f8041..eee2a2eefc 100644--- a/store/pipeline.go+++ b/store/pipeline.go@@ -183,7 +183,7 @@ func (s *Store) createPipelineRaw(ctx context.Context, create *api.PipelineCreatreturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.PipelineCache, pipeline.ID, pipeline); err != nil {+ if err := s.cache.UpsertCache(pipelineCacheNamespace, pipeline.ID, pipeline); err != nil {return nil, err}@@ -205,7 +205,7 @@ func (s *Store) findPipelineRaw(ctx context.Context, find *api.PipelineFind) ([]if err == nil {for _, pipeline := range list {- if err := s.cache.UpsertCache(api.PipelineCache, pipeline.ID, pipeline); err != nil {+ if err := s.cache.UpsertCache(pipelineCacheNamespace, pipeline.ID, pipeline); err != nil {return nil, err}}@@ -219,7 +219,7 @@ func (s *Store) findPipelineRaw(ctx context.Context, find *api.PipelineFind) ([]func (s *Store) getPipelineRaw(ctx context.Context, find *api.PipelineFind) (*pipelineRaw, error) {if find.ID != nil {pipelineRaw := &pipelineRaw{}- has, err := s.cache.FindCache(api.PipelineCache, *find.ID, pipelineRaw)+ has, err := s.cache.FindCache(pipelineCacheNamespace, *find.ID, pipelineRaw)if err != nil {return nil, err}@@ -244,7 +244,7 @@ func (s *Store) getPipelineRaw(ctx context.Context, find *api.PipelineFind) (*pi} else if len(pipelineRawList) > 1 {return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d pipelines with filter %+v, expect 1", len(pipelineRawList), find)}}- if err := s.cache.UpsertCache(api.PipelineCache, pipelineRawList[0].ID, pipelineRawList[0]); err != nil {+ if err := s.cache.UpsertCache(pipelineCacheNamespace, pipelineRawList[0].ID, pipelineRawList[0]); err != nil {return nil, err}return pipelineRawList[0], nil@@ -268,7 +268,7 @@ func (s *Store) patchPipelineRaw(ctx context.Context, patch *api.PipelinePatch)return nil, FormatError(err)}- if err := s.cache.UpsertCache(api.PipelineCache, pipelineRaw.ID, pipelineRaw); err != nil {+ if err := s.cache.UpsertCache(pipelineCacheNamespace, pipelineRaw.ID, pipelineRaw); err != nil {return nil, err}diff --git a/store/policy.go b/store/policy.goindex 67d383c3e8..fd0205603e 100644--- a/store/policy.go+++ b/store/policy.go@@ -67,7 +67,7 @@ func (s *Store) UpsertPolicy(ctx context.Context, upsert *api.PolicyUpsert) (*ap}// Cache environment tier policy as it is used widely.if upsert.Type == api.PolicyTypeEnvironmentTier {- if err := s.cache.UpsertCache(api.TierPolicyCache, upsert.ResourceID, policyRaw); err != nil {+ if err := s.cache.UpsertCache(tierPolicyCacheNamespace, upsert.ResourceID, policyRaw); err != nil {return nil, err}}@@ -131,7 +131,7 @@ func (s *Store) DeletePolicy(ctx context.Context, policyDelete *api.PolicyDelete}if policyDelete.Type == api.PolicyTypeEnvironmentTier {- s.cache.DeleteCache(api.TierPolicyCache, policyDelete.ResourceID)+ s.cache.DeleteCache(tierPolicyCacheNamespace, policyDelete.ResourceID)}return nil}@@ -228,7 +228,7 @@ func (s *Store) GetSQLReviewPolicyIDByEnvID(ctx context.Context, environmentID i// GetEnvironmentTierPolicyByEnvID will get the environment tier policy for an environment.func (s *Store) GetEnvironmentTierPolicyByEnvID(ctx context.Context, environmentID int) (*api.EnvironmentTierPolicy, error) {var policy *policyRaw- ok, err := s.cache.FindCache(api.TierPolicyCache, environmentID, &policy)+ ok, err := s.cache.FindCache(tierPolicyCacheNamespace, environmentID, &policy)if err != nil {return nil, err}@@ -244,7 +244,7 @@ func (s *Store) GetEnvironmentTierPolicyByEnvID(ctx context.Context, environment}policy = p// Cache the tier policy.- if err := s.cache.UpsertCache(api.TierPolicyCache, environmentID, policy); err != nil {+ if err := s.cache.UpsertCache(tierPolicyCacheNamespace, environmentID, policy); err != nil {return nil, err}}diff --git a/store/principal.go b/store/principal.goindex 0d67b6e432..2d575ab4a6 100644--- a/store/principal.go+++ b/store/principal.go@@ -155,7 +155,7 @@ func (s *Store) createPrincipalRaw(ctx context.Context, create *api.PrincipalCrereturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.PrincipalCache, principal.ID, principal); err != nil {+ if err := s.cache.UpsertCache(principalCacheNamespace, principal.ID, principal); err != nil {return nil, err}@@ -176,7 +176,7 @@ func (s *Store) findPrincipalRawList(ctx context.Context) ([]*principalRaw, erro}for _, principal := range list {- if err := s.cache.UpsertCache(api.PrincipalCache, principal.ID, principal); err != nil {+ if err := s.cache.UpsertCache(principalCacheNamespace, principal.ID, principal); err != nil {return nil, err}}@@ -189,7 +189,7 @@ func (s *Store) findPrincipalRawList(ctx context.Context) ([]*principalRaw, errofunc (s *Store) getPrincipalRaw(ctx context.Context, find *api.PrincipalFind) (*principalRaw, error) {if find.ID != nil {principalRaw := &principalRaw{}- has, err := s.cache.FindCache(api.PrincipalCache, *find.ID, principalRaw)+ has, err := s.cache.FindCache(principalCacheNamespace, *find.ID, principalRaw)if err != nil {return nil, err}@@ -214,7 +214,7 @@ func (s *Store) getPrincipalRaw(ctx context.Context, find *api.PrincipalFind) (*} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d principals with PrincipalFind[%+v], expect 1", len(list), find)}}- if err := s.cache.UpsertCache(api.PrincipalCache, list[0].ID, list[0]); err != nil {+ if err := s.cache.UpsertCache(principalCacheNamespace, list[0].ID, list[0]); err != nil {return nil, err}@@ -239,7 +239,7 @@ func (s *Store) patchPrincipalRaw(ctx context.Context, patch *api.PrincipalPatchreturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.PrincipalCache, principal.ID, principal); err != nil {+ if err := s.cache.UpsertCache(principalCacheNamespace, principal.ID, principal); err != nil {return nil, err}diff --git a/store/project.go b/store/project.goindex 5fb36caf8c..52dbd8a742 100644--- a/store/project.go+++ b/store/project.go@@ -200,7 +200,7 @@ func (s *Store) createProjectRaw(ctx context.Context, create *api.ProjectCreate)return nil, FormatError(err)}- if err := s.cache.UpsertCache(api.ProjectCache, projectRaw.ID, projectRaw); err != nil {+ if err := s.cache.UpsertCache(projectCacheNamespace, projectRaw.ID, projectRaw); err != nil {return nil, err}@@ -222,7 +222,7 @@ func (s *Store) findProjectRaw(ctx context.Context, find *api.ProjectFind) ([]*pif err == nil {for _, project := range list {- if err := s.cache.UpsertCache(api.ProjectCache, project.ID, project); err != nil {+ if err := s.cache.UpsertCache(projectCacheNamespace, project.ID, project); err != nil {return nil, err}}@@ -236,7 +236,7 @@ func (s *Store) findProjectRaw(ctx context.Context, find *api.ProjectFind) ([]*pfunc (s *Store) getProjectRaw(ctx context.Context, find *api.ProjectFind) (*projectRaw, error) {if find.ID != nil {project := &projectRaw{}- has, err := s.cache.FindCache(api.ProjectCache, *find.ID, project)+ has, err := s.cache.FindCache(projectCacheNamespace, *find.ID, project)if err != nil {return nil, err}@@ -261,7 +261,7 @@ func (s *Store) getProjectRaw(ctx context.Context, find *api.ProjectFind) (*proj} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d projects with filter %+v, expect 1", len(list), find)}}- if err := s.cache.UpsertCache(api.ProjectCache, list[0].ID, list[0]); err != nil {+ if err := s.cache.UpsertCache(projectCacheNamespace, list[0].ID, list[0]); err != nil {return nil, err}return list[0], nil@@ -285,7 +285,7 @@ func (s *Store) patchProjectRaw(ctx context.Context, patch *api.ProjectPatch) (*return nil, FormatError(err)}- if err := s.cache.UpsertCache(api.ProjectCache, project.ID, project); err != nil {+ if err := s.cache.UpsertCache(projectCacheNamespace, project.ID, project); err != nil {return nil, err}@@ -300,7 +300,7 @@ func (s *Store) patchProjectRawTx(ctx context.Context, tx *Tx, patch *api.Projecreturn nil, FormatError(err)}- if err := s.cache.UpsertCache(api.ProjectCache, project.ID, project); err != nil {+ if err := s.cache.UpsertCache(projectCacheNamespace, project.ID, project); err != nil {return nil, err}diff --git a/store/project_member.go b/store/project_member.goindex 49cbd45e57..50158104de 100644--- a/store/project_member.go+++ b/store/project_member.go@@ -67,7 +67,7 @@ func (s *Store) CreateProjectMember(ctx context.Context, create *api.ProjectMembreturn nil, errors.Wrapf(err, "failed to compose ProjectMember with projectMemberRaw[%+v]", projectMemberRaw)}// Invalidate the cache.- s.cache.DeleteCache(api.ProjectMemberCache, create.ProjectID)+ s.cache.DeleteCache(projectMemberCacheNamespace, create.ProjectID)return projectMember, nil}@@ -78,7 +78,7 @@ func (s *Store) FindProjectMember(ctx context.Context, find *api.ProjectMemberFifindCopy.ProjectID = nilisListProjectMember := find.ProjectID != nil && findCopy == api.ProjectMemberFind{}var cacheList []*api.ProjectMember- has, err := s.cache.FindCache(api.ProjectMemberCache, *find.ProjectID, &cacheList)+ has, err := s.cache.FindCache(projectMemberCacheNamespace, *find.ProjectID, &cacheList)if err != nil {return nil, err}@@ -99,7 +99,7 @@ func (s *Store) FindProjectMember(ctx context.Context, find *api.ProjectMemberFiprojectMemberList = append(projectMemberList, projectMember)}if isListProjectMember {- if err := s.cache.UpsertCache(api.ProjectMemberCache, *find.ProjectID, projectMemberList); err != nil {+ if err := s.cache.UpsertCache(projectMemberCacheNamespace, *find.ProjectID, projectMemberList); err != nil {return nil, err}}@@ -143,7 +143,7 @@ func (s *Store) PatchProjectMember(ctx context.Context, patch *api.ProjectMemberreturn nil, errors.Wrapf(err, "failed to compose ProjectMember with projectMemberRaw[%+v]", projectMemberRaw)}// Invalidate the cache.- s.cache.DeleteCache(api.ProjectMemberCache, projectMemberRaw.ProjectID)+ s.cache.DeleteCache(projectMemberCacheNamespace, projectMemberRaw.ProjectID)return projectMember, nil}@@ -163,7 +163,7 @@ func (s *Store) DeleteProjectMember(ctx context.Context, delete *api.ProjectMembreturn FormatError(err)}// Invalidate the cache.- s.cache.DeleteCache(api.ProjectMemberCache, delete.ProjectID)+ s.cache.DeleteCache(projectMemberCacheNamespace, delete.ProjectID)return nil}diff --git a/store/store.go b/store/store.goindex 66bf18a789..d4ef8203e7 100644--- a/store/store.go+++ b/store/store.go@@ -1,21 +1,17 @@// Package store is the implementation for managing Bytebase's own metadata in a PostgreSQL database.package store-import (- "github.com/bytebase/bytebase/api"-)-// Store provides database access to all raw objects.type Store struct {db *DB- cache api.CacheService+ cache *CacheService}// New creates a new instance of Store.-func New(db *DB, cache api.CacheService) *Store {+func New(db *DB) *Store {return &Store{db: db,- cache: cache,+ cache: newCacheService(),}}