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
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
}

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
}
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 {

View File

@ -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 := ""

View File

@ -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...)

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)
}
}

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"
}

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"
}

9
var.go
View File

@ -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{