add cache to framework

This commit is contained in:
hoteas
2021-05-24 05:47:56 +08:00
parent 253f836571
commit c8e926446e
9 changed files with 97 additions and 29 deletions
+8 -7
View File
@@ -4,15 +4,16 @@ import (
"../../../hotime"
"../mysql"
"../sqlite"
"strings"
)
func SetDB(appIns *hotime.Application) {
if appIns.Config.GetString("dbType") == "sqlite" || strings.Index(appIns.Config.GetString("dbName"), ".db") == len(appIns.Config.GetString("dbName"))-3 {
appIns.Config["dbType"] = "sqlite"
sqlite.SetDB(appIns)
} else {
appIns.Config["dbType"] = "mysql"
mysql.SetDB(appIns)
db := appIns.Config.GetMap("db")
dbSqlite := db.GetMap("sqlite")
dbMysql := db.GetMap("mysql")
if db != nil && dbSqlite != nil {
sqlite.SetDB(appIns, dbSqlite)
}
if db != nil && dbMysql != nil {
mysql.SetDB(appIns, dbMysql)
}
}
+4 -3
View File
@@ -6,14 +6,15 @@ import (
_ "github.com/go-sql-driver/mysql"
)
func SetDB(appIns *hotime.Application) {
func SetDB(appIns *hotime.Application, config hotime.Map) {
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"
query := config.GetString("user") + ":" + config.GetString("password") +
"@tcp(" + config.GetString("host") + ":" + config.GetString("port") + ")/" + config.GetString("name") + "?charset=utf8"
DB, e := sql.Open("mysql", query)
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
return DB
})
appIns.Db.Type = "mysql"
}
+3 -2
View File
@@ -6,12 +6,13 @@ import (
_ "github.com/mattn/go-sqlite3"
)
func SetDB(appIns *hotime.Application) {
func SetDB(appIns *hotime.Application, config hotime.Map) {
appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB {
db, e := sql.Open("sqlite3", appIns.Config.GetString("dbName"))
db, e := sql.Open("sqlite3", config.GetString("path"))
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
return db
})
appIns.Db.Type = "sqlite"
}