mysql add master and slave mode

This commit is contained in:
hoteas
2021-05-24 06:14:58 +08:00
parent c8e926446e
commit 2632517fab
7 changed files with 81 additions and 86 deletions
+2 -16
View File
@@ -2,7 +2,6 @@ package main
import (
"../../hotime"
"../tools/db"
"database/sql"
"fmt"
//"go.hoteas.com/hotime/cache"
@@ -34,32 +33,19 @@ func main() {
//mysql
//mysql.SetDB(&appIns)
//自动选择数据库
db.SetDB(&appIns)
//appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB {
// query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") +
// "@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8"
// DB, e := sql.Open("mysql", query)
// if e != nil && len(err) != 0 {
// err[0].SetError(e)
// }
// return DB
//})
//内存缓存数据库数据,错误则删除
// appIns.Db.CacheIns=hotime.CacheIns(&hotime.CacheMemory{})
appIns.SetSession(hotime.CacheIns(&hotime.CacheMemory{}), hotime.CacheIns(&hotime.CacheDb{Db: &appIns.Db, Time: appIns.Config.GetInt64("cacheTime")}))
appIns.SetCache(hotime.CacheIns(&hotime.CacheMemory{}))
//快捷模式
appIns.SetDefault(func(err ...*hotime.Error) *sql.DB {
appIns.SetDefault(func(err ...*hotime.Error) (*sql.DB, *sql.DB) {
query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") +
"@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8"
DB, e := sql.Open("mysql", query)
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
return DB
return DB, nil
})
appIns.Run(hotime.Router{