add cache to framework
This commit is contained in:
parent
253f836571
commit
c8e926446e
@ -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
27
cache.go
Normal 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
8
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 {
|
||||
|
25
objtoobj.go
25
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 := ""
|
||||
|
9
slice.go
9
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...)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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"
|
||||
}
|
||||
|
@ -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
9
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{
|
||||
|
Loading…
Reference in New Issue
Block a user