From c8e926446edc66d723eeb8c398c02ae9ebd245b4 Mon Sep 17 00:00:00 2001 From: hoteas Date: Mon, 24 May 2021 05:47:56 +0800 Subject: [PATCH] add cache to framework --- application.go | 21 +++++++++------------ cache.go | 27 +++++++++++++++++++++++++++ map.go | 8 ++++++++ objtoobj.go | 25 +++++++++++++++++++++++++ slice.go | 9 +++++++++ tools/db/auto.go | 15 ++++++++------- tools/mysql/mysql.go | 7 ++++--- tools/sqlite/sqlite.go | 5 +++-- var.go | 9 ++++----- 9 files changed, 97 insertions(+), 29 deletions(-) create mode 100644 cache.go diff --git a/application.go b/application.go index 01d0a03..bad8ab4 100644 --- a/application.go +++ b/application.go @@ -1,6 +1,7 @@ package hotime import ( + "./tools/db" "bytes" "database/sql" "encoding/json" @@ -158,21 +159,9 @@ func (this *Application) Run(router Router) { //启动实例 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.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) } } +func Init(config string) Application { + appIns := Application{} + //手动模式, + appIns.SetConfig(config) + db.SetDB(&appIns) + //appIns.SetCache() + return appIns +} diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..6127dc7 --- /dev/null +++ b/cache.go @@ -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 +} diff --git a/map.go b/map.go index 1d9751d..a4ae5f6 100644 --- a/map.go +++ b/map.go @@ -90,6 +90,14 @@ func (this Map) GetSlice(key string, err ...*Error) Slice { 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 { diff --git a/objtoobj.go b/objtoobj.go index d9f5419..3653677 100644 --- a/objtoobj.go +++ b/objtoobj.go @@ -212,6 +212,31 @@ func ObjToInt(obj interface{}, e ...*Error) int { 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 { // fmt.Println(reflect.ValueOf(obj).Type().String() ) str := "" diff --git a/slice.go b/slice.go index 7cf9932..034cca9 100644 --- a/slice.go +++ b/slice.go @@ -60,6 +60,15 @@ func (this Slice) GetSlice(key int, err ...*Error) Slice { 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 { //var v Map v := ObjToMap((this)[key], err...) diff --git a/tools/db/auto.go b/tools/db/auto.go index 42938ef..059bbcd 100644 --- a/tools/db/auto.go +++ b/tools/db/auto.go @@ -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) } } diff --git a/tools/mysql/mysql.go b/tools/mysql/mysql.go index 7942286..3e420fa 100644 --- a/tools/mysql/mysql.go +++ b/tools/mysql/mysql.go @@ -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" } diff --git a/tools/sqlite/sqlite.go b/tools/sqlite/sqlite.go index e101bc5..69786c6 100644 --- a/tools/sqlite/sqlite.go +++ b/tools/sqlite/sqlite.go @@ -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" } diff --git a/var.go b/var.go index be6e912..08a03c3 100644 --- a/var.go +++ b/var.go @@ -27,11 +27,10 @@ var Config = Map{ "4": "数据处理异常", "5": "数据结果异常", }, - "tpt": "tpt", - "defFile": []string{"index.html", "index.htm"}, - "modeRouterStrict": false, - "port": "80", - "sessionName": "HOTIME", + "tpt": "tpt", + "defFile": []string{"index.html", "index.htm"}, + "port": "80", + "sessionName": "HOTIME", } var ConfigNote = Map{