12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package mdb
- import (
- "github.com/go-redis/redis/v8"
- mhayaMongo "github.com/mhaya/components/mongo"
- clog "github.com/mhaya/logger"
- cactor "github.com/mhaya/net/actor"
- cprofile "github.com/mhaya/profile"
- "go.mongodb.org/mongo-driver/mongo"
- )
- type ActorDB struct {
- cactor.Base
- }
- var (
- MDB *mongo.Database
- RDB redis.UniversalClient
- )
- func (p *ActorDB) AliasID() string {
- return "db"
- }
- // OnInit Actor初始化前触发该函数
- func (p *ActorDB) OnInit() {
- mongo := p.App().Find(mhayaMongo.Name).(*mhayaMongo.Component)
- if mongo == nil {
- clog.DPanicf("[component = %s] not found.", mhayaMongo.Name)
- }
- // 获取 db_id = "center_db_1" 的配置
- dbID := p.App().Settings().GetConfig("db_id_list").GetString("game_db_id")
- MDB = mongo.GetDb(dbID)
- if MDB == nil {
- clog.Panic("game_db_id not found")
- }
- redisConfig := cprofile.GetConfig("redis")
- RDB = redis.NewUniversalClient(&redis.UniversalOptions{
- Addrs: []string{redisConfig.GetString("address")},
- Password: redisConfig.GetString("password"),
- DB: redisConfig.GetInt("db"),
- })
- if p.App().NodeId() == "m-center" {
- SetIndex()
- }
- }
|