77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
package db
|
|
|
|
import (
|
|
"go_dreamfactory/lego/sys/log"
|
|
"sync"
|
|
)
|
|
|
|
func newSys(options *Options) (sys *DB, err error) {
|
|
sys = &DB{
|
|
options: options,
|
|
data: make(map[string]*ModelDataExpired),
|
|
}
|
|
if err = sys.init(); err != nil {
|
|
go sys.run()
|
|
}
|
|
return
|
|
}
|
|
|
|
type DB struct {
|
|
options *Options
|
|
local *DBConn
|
|
cross *DBConn
|
|
servers map[string]*DBConn
|
|
mu sync.RWMutex
|
|
data map[string]*ModelDataExpired //过期数据
|
|
}
|
|
|
|
func (this *DB) init() (err error) {
|
|
if this.local, err = newDBConn(this.options.Loacl); err != nil {
|
|
return
|
|
}
|
|
if this.options.Cross.Enabled {
|
|
if this.cross, err = newDBConn(this.options.Cross); err != nil {
|
|
return
|
|
}
|
|
}
|
|
if this.options.ServerList != nil && len(this.options.ServerList) > 0 {
|
|
this.servers = make(map[string]*DBConn, len(this.options.ServerList))
|
|
for k, v := range this.options.ServerList {
|
|
if this.servers[k], err = newDBConn(v); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *DB) Local() *DBConn {
|
|
if this.local == nil {
|
|
log.Panic("LocalDBConn on init")
|
|
}
|
|
return this.local
|
|
}
|
|
|
|
func (this *DB) Cross() *DBConn {
|
|
if this.cross == nil {
|
|
log.Panic("CrossDBConn on init")
|
|
}
|
|
return this.cross
|
|
}
|
|
|
|
func (this *DB) ServerDBConn(stage string) (conn *DBConn) {
|
|
ok := false
|
|
conn, ok = this.servers[stage]
|
|
if !ok {
|
|
log.Panicf("DBConn:%s on init", stage)
|
|
}
|
|
return
|
|
}
|
|
func (this *DB) GetServerTags() []string {
|
|
keys := make([]string, 0)
|
|
for k, _ := range this.servers {
|
|
keys = append(keys, k)
|
|
}
|
|
return keys
|
|
}
|