Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize database connection #28

Open
Wenrh2004 opened this issue Aug 14, 2023 · 1 comment
Open

Optimize database connection #28

Wenrh2004 opened this issue Aug 14, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@Wenrh2004
Copy link
Owner

将mongodb、redis数据库连接整合至globeDatabaseConfig

@Wenrh2004 Wenrh2004 added the enhancement New feature or request label Aug 14, 2023
@Wenrh2004 Wenrh2004 self-assigned this Aug 14, 2023
@ha0min
Copy link
Collaborator

ha0min commented Aug 15, 2023

或者数据库都放在repo目录里面加全局变量?

var RDB redis.Conn

func init() {
    // gorm initalization here...
     initRedis()
}

// 初始化redis配置信息
func initRedis() {
    log.NewLog("info").Info("初始化Redis...")
    addr := config.AppConfig.Redis.Host + config.AppConfig.Redis.Port
    RD, err := redis.Dial("tcp", addr, redis.DialPassword(config.AppConfig.Redis.Password))
    if err != nil {
        log.NewLog(constants.ERROR).Error("Redis连接失败...", err)
        panic(err)
    }
    RDB = RD
    rand.Seed(time.Now().UnixNano())
    log.NewLog(constants.INFO).Info("初始化Redis结束...", RDB)
}

使用直接repo.RDB就能自动init()启动所有数据库连接,比在globalDatabaseConfig里面再手动调用Get会好一些

Repository owner deleted a comment from mahmoudfranc Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants