optimize dao initial logic

pull/351/head
Michael Li 2 years ago
parent e25faa8a3c
commit 57f6a43252
No known key found for this signature in database

@ -20,31 +20,75 @@ import (
var (
ts core.TweetSearchService
ds core.DataService
dsa core.DataServantA
oss core.ObjectStorageService
onceTs, onceDs, onceOss sync.Once
_onceInitial sync.Once
)
func DataService() core.DataService {
onceDs.Do(func() {
var v core.VersionInfo
lazyInitial()
return ds
}
func DataServantA() core.DataServantA {
lazyInitial()
return dsa
}
func ObjectStorageService() core.ObjectStorageService {
lazyInitial()
return oss
}
func TweetSearchService() core.TweetSearchService {
lazyInitial()
return ts
}
func newAuthorizationManageService() (ams core.AuthorizationManageService) {
if cfg.If("Gorm") {
ds, v = jinzhu.NewDataService()
ams = jinzhu.NewAuthorizationManageService()
} else if cfg.If("Sqlx") {
ds, v = sakila.NewDataService()
ams = sakila.NewAuthorizationManageService()
} else if cfg.If("Sqlc") && (cfg.If("Postgres") || cfg.If("PostgreSQL")) {
ds, v = slonik.NewDataService()
ams = slonik.NewAuthorizationManageService()
} else {
// default use gorm as orm for sql database
ds, v = jinzhu.NewDataService()
ams = jinzhu.NewAuthorizationManageService()
}
logrus.Infof("use %s as data service with version %s", v.Name(), v.Version())
return
}
// lazyInitial do some package lazy initialize for performance
func lazyInitial() {
_onceInitial.Do(func() {
initDsX()
initOSS()
initTsX()
})
return ds
}
func ObjectStorageService() core.ObjectStorageService {
onceOss.Do(func() {
func initDsX() {
var dsVer, dsaVer core.VersionInfo
if cfg.If("Gorm") {
ds, dsVer = jinzhu.NewDataService()
dsa, dsaVer = jinzhu.NewDataServantA()
} else if cfg.If("Sqlx") {
ds, dsVer = sakila.NewDataService()
dsa, dsaVer = sakila.NewDataServantA()
} else if cfg.If("Sqlc") && (cfg.If("Postgres") || cfg.If("PostgreSQL")) {
ds, dsVer = slonik.NewDataService()
dsa, dsaVer = slonik.NewDataServantA()
} else {
// default use gorm as orm for sql database
ds, dsVer = jinzhu.NewDataService()
dsa, dsaVer = jinzhu.NewDataServantA()
}
logrus.Infof("use %s as core.DataService with version %s", dsVer.Name(), dsVer.Version())
logrus.Infof("use %s as core.ServantA with version %s", dsaVer.Name(), dsaVer.Version())
}
func initOSS() {
var v core.VersionInfo
if cfg.If("AliOSS") {
oss, v = storage.MustAliossService()
@ -67,38 +111,21 @@ func ObjectStorageService() core.ObjectStorageService {
return
}
logrus.Infof("use %s as object storage by version %s", v.Name(), v.Version())
})
return oss
}
func TweetSearchService() core.TweetSearchService {
onceTs.Do(func() {
func initTsX() {
var v core.VersionInfo
ams := newAuthorizationManageService()
if cfg.If("Zinc") {
cfg.On(cfg.Actions{
"Zinc": func() {
ts, v = search.NewZincTweetSearchService(ams)
} else if cfg.If("Meili") {
},
"Meili": func() {
ts, v = search.NewMeiliTweetSearchService(ams)
} else {
// default use Zinc as tweet search service
},
}, func() {
ts, v = search.NewZincTweetSearchService(ams)
}
})
logrus.Infof("use %s as tweet search serice by version %s", v.Name(), v.Version())
ts = search.NewBridgeTweetSearchService(ts)
})
return ts
}
func newAuthorizationManageService() (s core.AuthorizationManageService) {
if cfg.If("Gorm") {
s = jinzhu.NewAuthorizationManageService()
} else if cfg.If("Sqlx") {
s = sakila.NewAuthorizationManageService()
} else if cfg.If("Sqlc") && (cfg.If("Postgres") || cfg.If("PostgreSQL")) {
s = slonik.NewAuthorizationManageService()
} else {
s = jinzhu.NewAuthorizationManageService()
}
return
}

@ -21,13 +21,16 @@ import (
)
var (
_ core.DataService = (*dataServant)(nil)
_ core.VersionInfo = (*dataServant)(nil)
_ core.DataService = (*dataSrv)(nil)
_ core.VersionInfo = (*dataSrv)(nil)
_ core.DataServantA = (*dataSrvA)(nil)
_ core.VersionInfo = (*dataSrvA)(nil)
_onceInitial sync.Once
)
type dataServant struct {
type dataSrv struct {
core.IndexPostsService
core.WalletService
core.MessageService
@ -43,6 +46,13 @@ type dataServant struct {
core.AttachmentCheckService
}
type dataSrvA struct {
core.TopicServantA
core.TweetServantA
core.TweetManageServantA
core.TweetHelpServantA
}
func NewDataService() (core.DataService, core.VersionInfo) {
lazyInitial()
@ -81,7 +91,7 @@ func NewDataService() (core.DataService, core.VersionInfo) {
}
logrus.Infof("use %s as cache index service by version: %s", v.Name(), v.Version())
ds := &dataServant{
ds := &dataSrv{
IndexPostsService: cis,
WalletService: newWalletService(db),
MessageService: newMessageService(db),
@ -99,18 +109,40 @@ func NewDataService() (core.DataService, core.VersionInfo) {
return ds, ds
}
func NewDataServantA() (core.DataServantA, core.VersionInfo) {
lazyInitial()
db := conf.MustGormDB()
ds := &dataSrvA{
TopicServantA: newTopicServantA(db),
TweetServantA: newTweetServantA(db),
TweetManageServantA: newTweetManageServantA(db),
TweetHelpServantA: newTweetHelpServantA(db),
}
return ds, ds
}
func NewAuthorizationManageService() core.AuthorizationManageService {
return newAuthorizationManageService(conf.MustGormDB())
}
func (s *dataServant) Name() string {
func (s *dataSrv) Name() string {
return "Gorm"
}
func (s *dataServant) Version() *semver.Version {
func (s *dataSrv) Version() *semver.Version {
return semver.MustParse("v0.2.0")
}
func (s *dataSrvA) Name() string {
return "Gorm"
}
func (s *dataSrvA) Version() *semver.Version {
return semver.MustParse("v0.1.0")
}
// lazyInitial do some package lazy initialize for performance
func lazyInitial() {
_onceInitial.Do(func() {

@ -43,7 +43,6 @@ type tweetSrvA struct {
}
type tweetManageSrvA struct {
cacheIndex core.CacheIndexService
db *gorm.DB
}
@ -70,6 +69,24 @@ func newTweetHelpService(db *gorm.DB) core.TweetHelpService {
}
}
func newTweetServantA(db *gorm.DB) core.TweetServantA {
return &tweetSrvA{
db: db,
}
}
func newTweetManageServantA(db *gorm.DB) core.TweetManageServantA {
return &tweetManageSrvA{
db: db,
}
}
func newTweetHelpServantA(db *gorm.DB) core.TweetHelpServantA {
return &tweetHelpSrvA{
db: db,
}
}
// MergePosts post数据整合
func (s *tweetHelpSrv) MergePosts(posts []*core.Post) ([]*core.PostFormated, error) {
postIds := make([]int64, 0, len(posts))

@ -17,6 +17,11 @@ func NewDataService() (core.DataService, core.VersionInfo) {
return nil, nil
}
func NewDataServantA() (core.DataServantA, core.VersionInfo) {
logrus.Fatal("not support now")
return nil, nil
}
func NewAuthorizationManageService() core.AuthorizationManageService {
logrus.Fatal("not support now")
return nil

@ -17,6 +17,11 @@ func NewDataService() (core.DataService, core.VersionInfo) {
return nil, nil
}
func NewDataServantA() (core.DataServantA, core.VersionInfo) {
logrus.Fatal("not support now")
return nil, nil
}
func NewAuthorizationManageService() core.AuthorizationManageService {
logrus.Fatal("not support now")
return nil

@ -14,7 +14,9 @@ import (
"github.com/alimy/mir/v3"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/internal/dao"
"github.com/rocboss/paopao-ce/pkg/app"
"github.com/rocboss/paopao-ce/pkg/types"
"github.com/rocboss/paopao-ce/pkg/xerror"
@ -24,6 +26,7 @@ type BaseServant types.Empty
type DaoServant struct {
Redis *redis.Client
Dsa core.DataServantA
Ds core.DataService
Ts core.TweetSearchService
}
@ -113,6 +116,15 @@ func RenderAny(c *gin.Context, data any, err mir.Error) {
}
}
func NewDaoServant() *DaoServant {
return &DaoServant{
Redis: conf.MustRedis(),
Dsa: dao.DataServantA(),
Ds: dao.DataService(),
Ts: dao.TweetSearchService(),
}
}
func (s *DaoServant) GetTweetBy(id int64) (*core.PostFormated, error) {
post, err := s.Ds.GetPostByID(id)
if err != nil {

@ -26,11 +26,7 @@ var (
func RouteWeb(e *gin.Engine) {
lazyInitial()
oss := dao.ObjectStorageService()
ds := &base.DaoServant{
Redis: conf.MustRedis(),
Ds: dao.DataService(),
Ts: dao.TweetSearchService(),
}
ds := base.NewDaoServant()
// aways register servants
api.RegisterAdminServant(e, newAdminSrv(ds), newAdminBinding(), newAdminRender())
api.RegisterCoreServant(e, newCoreSrv(ds, oss), newCoreBinding(), newCoreRender())

Loading…
Cancel
Save