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

View File

@ -1,6 +1,7 @@
package hotime package hotime
import ( import (
"./tools/db"
"bytes" "bytes"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
@ -158,21 +159,9 @@ func (this *Application) Run(router Router) {
//启动实例 //启动实例
func (this *Application) SetConnectDB(connect func(err ...*Error) *sql.DB) { func (this *Application) SetConnectDB(connect func(err ...*Error) *sql.DB) {
//this.Db.DBCached=false
//if this.Config.GetCeilInt("dbCached")!=0{
// this.Db.DBCached=true
//}
this.connectDbFunc = connect this.connectDbFunc = connect
this.Db.SetConnect(this.connectDbFunc) this.Db.SetConnect(this.connectDbFunc)
this.Db.DBCached = false
if this.Config.GetCeilInt("dbCached") != 0 {
this.Db.DBCached = true
}
this.Db.Type = this.Config.GetString("dbType")
} }
//设置配置文件路径全路径或者相对路径 //设置配置文件路径全路径或者相对路径
@ -458,3 +447,11 @@ func (this *Application) crossDomain(context *Context) {
header.Set("Access-Control-Allow-Origin", refer) header.Set("Access-Control-Allow-Origin", refer)
} }
} }
func Init(config string) Application {
appIns := Application{}
//手动模式,
appIns.SetConfig(config)
db.SetDB(&appIns)
//appIns.SetCache()
return appIns
}

27
cache.go Normal file
View File

@ -0,0 +1,27 @@
package hotime
type HoTimeCache struct {
//set(key string, value interface{}, time int64)
//get(key string) interface{}
//delete(key string)
dbCache CacheIns
redisCache CacheIns
memoryCache CacheIns
}
func (this *HoTimeCache) Session(key string, data ...interface{}) *Obj {
return nil
}
func (this *HoTimeCache) Db(key string, data ...interface{}) *Obj {
return nil
}
func (this *HoTimeCache) Cache(key string, data ...interface{}) *Obj {
return nil
}
func (this *HoTimeCache) getIndex() []CacheIns {
return nil
}

8
map.go
View File

@ -90,6 +90,14 @@ func (this Map) GetSlice(key string, err ...*Error) Slice {
return v return v
}
func (this Map) GetBool(key string, err ...*Error) bool {
//var v Slice
v := ObjToBool((this)[key], err...)
return v
} }
func (this Map) GetMap(key string, err ...*Error) Map { func (this Map) GetMap(key string, err ...*Error) Map {

View File

@ -212,6 +212,31 @@ func ObjToInt(obj interface{}, e ...*Error) int {
return int(v) return int(v)
} }
func ObjToBool(obj interface{}, e ...*Error) bool {
var err error
v := false
if obj == nil {
err = errors.New("没有合适的转换对象!")
} else {
switch obj.(type) {
case bool:
v = obj.(bool)
default:
toInt := ObjToInt(obj)
if toInt != 0 {
v = true
}
err = errors.New("没有合适的转换对象!")
}
}
if len(e) != 0 {
e[0].SetError(err)
}
return v
}
func ObjToStr(obj interface{}) string { func ObjToStr(obj interface{}) string {
// fmt.Println(reflect.ValueOf(obj).Type().String() ) // fmt.Println(reflect.ValueOf(obj).Type().String() )
str := "" str := ""

View File

@ -60,6 +60,15 @@ func (this Slice) GetSlice(key int, err ...*Error) Slice {
return v return v
} }
func (this Slice) GetBool(key int, err ...*Error) bool {
//var v Slice
v := ObjToBool((this)[key], err...)
return v
}
func (this Slice) GetMap(key int, err ...*Error) Map { func (this Slice) GetMap(key int, err ...*Error) Map {
//var v Map //var v Map
v := ObjToMap((this)[key], err...) v := ObjToMap((this)[key], err...)

View File

@ -4,15 +4,16 @@ import (
"../../../hotime" "../../../hotime"
"../mysql" "../mysql"
"../sqlite" "../sqlite"
"strings"
) )
func SetDB(appIns *hotime.Application) { func SetDB(appIns *hotime.Application) {
if appIns.Config.GetString("dbType") == "sqlite" || strings.Index(appIns.Config.GetString("dbName"), ".db") == len(appIns.Config.GetString("dbName"))-3 { db := appIns.Config.GetMap("db")
appIns.Config["dbType"] = "sqlite" dbSqlite := db.GetMap("sqlite")
sqlite.SetDB(appIns) dbMysql := db.GetMap("mysql")
} else { if db != nil && dbSqlite != nil {
appIns.Config["dbType"] = "mysql" sqlite.SetDB(appIns, dbSqlite)
mysql.SetDB(appIns) }
if db != nil && dbMysql != nil {
mysql.SetDB(appIns, dbMysql)
} }
} }

View File

@ -6,14 +6,15 @@ import (
_ "github.com/go-sql-driver/mysql" _ "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 { appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB {
query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") + query := config.GetString("user") + ":" + config.GetString("password") +
"@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8" "@tcp(" + config.GetString("host") + ":" + config.GetString("port") + ")/" + config.GetString("name") + "?charset=utf8"
DB, e := sql.Open("mysql", query) DB, e := sql.Open("mysql", query)
if e != nil && len(err) != 0 { if e != nil && len(err) != 0 {
err[0].SetError(e) err[0].SetError(e)
} }
return DB return DB
}) })
appIns.Db.Type = "mysql"
} }

View File

@ -6,12 +6,13 @@ import (
_ "github.com/mattn/go-sqlite3" _ "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 { 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 { if e != nil && len(err) != 0 {
err[0].SetError(e) err[0].SetError(e)
} }
return db return db
}) })
appIns.Db.Type = "sqlite"
} }

1
var.go
View File

@ -29,7 +29,6 @@ var Config = Map{
}, },
"tpt": "tpt", "tpt": "tpt",
"defFile": []string{"index.html", "index.htm"}, "defFile": []string{"index.html", "index.htm"},
"modeRouterStrict": false,
"port": "80", "port": "80",
"sessionName": "HOTIME", "sessionName": "HOTIME",
} }