From 048c4573033593246ef4ad43e92053bfd91087a7 Mon Sep 17 00:00:00 2001 From: hoteas <925970985@qq.com> Date: Sun, 10 Nov 2019 18:00:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 17 ++++++-- application.go | 62 +++++++++++++---------------- cache_db.go | 56 +++++++++++++++++++------- db.go | 96 +++++++++++++++++++++------------------------ 4 files changed, 127 insertions(+), 104 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9bfdd23..6e9c776 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,10 @@ - + + + + diff --git a/application.go b/application.go index 1341adc..c0d0480 100644 --- a/application.go +++ b/application.go @@ -7,12 +7,12 @@ import ( "errors" "fmt" "io/ioutil" + "log" "net/http" "net/url" "os" "path/filepath" "strconv" - "log" "strings" ) @@ -21,7 +21,7 @@ type Application struct { Router contextBase Port string //端口号 - TLSPort string //ssl访问端口号 + TLSPort string //ssl访问端口号 connectListener []func(this *Context) bool //所有的访问监听,true按原计划继续使用,false表示有监听器处理 connectDbFunc func(err ...*Error) *sql.DB configPath string @@ -97,47 +97,38 @@ func (this *Application) Run(router Router) { IsRun = true } - - - ch := make(chan int) - if ObjToCeilInt(this.Port)!=0{ - go func() { - + if ObjToCeilInt(this.Port) != 0 { + go func() { App[this.Port] = this this.Server.Handler = this //启动服务 this.Server.Addr = ":" + this.Port - err:=this.Server.ListenAndServe() + err := this.Server.ListenAndServe() log.Println(err) - ch <-1 - - - - }() - }else if ObjToCeilInt(this.TLSPort)!=0{ - go func() { - - App[this.TLSPort] = this - this.Server.Handler = this - //启动服务 - this.Server.Addr = ":" + this.TLSPort - err:=this.Server.ListenAndServeTLS(this.Config.GetString("tlsCert"),this.Config.GetString("tlsKey")) - log.Println(err) - ch <-2 - - + ch <- 1 }() - }else{ + } else if ObjToCeilInt(this.TLSPort) != 0 { + go func() { + + App[this.TLSPort] = this + this.Server.Handler = this + //启动服务 + this.Server.Addr = ":" + this.TLSPort + err := this.Server.ListenAndServeTLS(this.Config.GetString("tlsCert"), this.Config.GetString("tlsKey")) + log.Println(err) + ch <- 2 + + }() + } else { log.Println("没有端口启用") return } - - value := <- ch + value := <-ch log.Println("启动服务失败 : ", value) } @@ -153,11 +144,13 @@ func (this *Application) SetConnectDB(connect func(err ...*Error) *sql.DB) { this.connectDbFunc = connect this.Db.SetConnect(this.connectDbFunc) - this.Db.DBCached=false - if this.Config.GetCeilInt("dbCached")!=0{ - this.Db.DBCached=true + this.Db.DBCached = false + if this.Config.GetCeilInt("dbCached") != 0 { + this.Db.DBCached = true } + this.Db.Type = this.Config.GetString("dbType") + } //设置配置文件路径全路径或者相对路径 @@ -220,7 +213,7 @@ func (this *Application) SetConfig(configPath ...string) { this.Config.Put(k, v) //程序配置 Config.Put(k, v) //系统配置 } - }else { + } else { log.Println("配置文件不存在,或者配置出错,使用缺省默认配置") } @@ -232,11 +225,10 @@ func (this *Application) SetConfig(configPath ...string) { err = json.Indent(&out, []byte(this.Config.ToJsonString()), "", "\t") //判断配置文件是否序列有变化有则修改配置,五则不变 //fmt.Println(len(btes)) - if len(btes)!=0&&out.String()==string(btes){ + if len(btes) != 0 && out.String() == string(btes) { return } - err = ioutil.WriteFile(this.configPath, out.Bytes(), os.ModeAppend) if err != nil { diff --git a/cache_db.go b/cache_db.go index 1752cae..b599521 100644 --- a/cache_db.go +++ b/cache_db.go @@ -2,8 +2,8 @@ package hotime import ( "encoding/json" - "time" "strings" + "time" ) type CacheDb struct { @@ -17,18 +17,46 @@ func (this *CacheDb) initDbTable() { if this.isInit { return } + if this.Db.Type == "mysql" { - dbNames := this.Db.Query("SELECT DATABASE()") + dbNames := this.Db.Query("SELECT DATABASE()") + + if len(dbNames) == 0 { + return + } + dbName := dbNames[0].GetString("DATABASE()") + res := this.Db.Query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + dbName + "' AND TABLE_NAME='cached'") + if len(res) != 0 { + this.isInit = true + return + } + + _, e := this.Db.Exec("CREATE TABLE `cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ckey` varchar(60) DEFAULT NULL, `cvalue` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8") + if e.GetError() == nil { + this.isInit = true + } - if len(dbNames) == 0 { - return } - dbName := dbNames[0].GetString("DATABASE()") - res := this.Db.Query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + dbName + "' AND TABLE_NAME='cached'") - if len(res) == 0 { - this.Db.Exec("CREATE TABLE `cached` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `ckey` varchar(60) DEFAULT NULL, `cvalue` varchar(2000) DEFAULT NULL, `time` bigint(20) DEFAULT NULL, `endtime` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=198740 DEFAULT CHARSET=utf8") + + if this.Db.Type == "sqlite" { + res := this.Db.Query(`select * from sqlite_master where type = 'table' and name = 'cached'`) + + if len(res) != 0 { + this.isInit = true + return + } + _, e := this.Db.Exec(`CREATE TABLE "cached" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "ckey" TEXT(60), + "cvalue" TEXT(2000), + "time" integer, + "endtime" integer + );`) + if e.GetError() == nil { + this.isInit = true + } + } - this.isInit = true } @@ -71,13 +99,13 @@ func (this *CacheDb) set(key string, value interface{}, tim int64) { func (this *CacheDb) delete(key string) { - del:=strings.Index(key,"*") + del := strings.Index(key, "*") //如果通配删除 - if del!=-1{ - key=Substr(key,0,del) - this.Db.Delete("cached", Map{"ckey": key+"%"}) + if del != -1 { + key = Substr(key, 0, del) + this.Db.Delete("cached", Map{"ckey": key + "%"}) - }else{ + } else { this.Db.Delete("cached", Map{"ckey": key}) } } diff --git a/db.go b/db.go index dfd2dbe..1bd52d5 100644 --- a/db.go +++ b/db.go @@ -13,6 +13,7 @@ type HoTimeDB struct { *sql.DB contextBase CacheIns + Type string DBCached bool LastQuery string LastData []interface{} @@ -31,7 +32,7 @@ func (this *HoTimeDB) SetConnect(connect func(err ...*Error) *sql.DB, err ...*Er //事务,如果action返回true则执行成功;false则回滚 func (this *HoTimeDB) Action(action func(db HoTimeDB) bool) bool { - db := HoTimeDB{DB: this.DB,CacheIns:this.CacheIns,DBCached:this.DBCached} + db := HoTimeDB{DB: this.DB, CacheIns: this.CacheIns, DBCached: this.DBCached} tx, err := db.Begin() if err != nil { this.LastErr.SetError(err) @@ -120,11 +121,11 @@ func (this *HoTimeDB) Row(resl *sql.Rows) []Map { } //防止int被误读为float64 - jlis,e:=json.Marshal(lis) - if e!=nil{ + jlis, e := json.Marshal(lis) + if e != nil { this.LastErr.SetError(e) - }else{ - lis.JsonToMap(string(jlis),&this.LastErr) + } else { + lis.JsonToMap(string(jlis), &this.LastErr) } dest = append(dest, lis) @@ -433,37 +434,30 @@ func (this *HoTimeDB) Select(table string, qu ...interface{}) []Map { query += temp qs = append(qs, resWhere...) - md5:=this.md5(query,qs...) + md5 := this.md5(query, qs...) - if this.DBCached&&this.CacheIns!=nil{ + if this.DBCached && this.CacheIns != nil { //如果缓存有则从缓存取 - cacheData:=this.Cache(table+":"+md5) + cacheData := this.Cache(table + ":" + md5) - if cacheData.Data!=nil{ + if cacheData.Data != nil { return cacheData.ToMapArray() } } - - //无缓存则数据库取 res := this.Query(query, qs...) - - if res == nil { - res=[]Map{} + res = []Map{} } //缓存 - if this.DBCached&&this.CacheIns!=nil{ + if this.DBCached && this.CacheIns != nil { - this.Cache(table+":"+md5,res) + this.Cache(table+":"+md5, res) } - - - return res } @@ -592,7 +586,6 @@ func (this *HoTimeDB) where(data Map) (string, []interface{}) { } - } else { //fmt.Println(v) where += " " + ObjToStr(v) @@ -641,17 +634,17 @@ func (this *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { where += "`" + k + "` LIKE ? " v = "%" + ObjToStr(v) + "%" res = append(res, v) - case "[!~]"://左边任意 + case "[!~]": //左边任意 k = strings.Replace(k, "[~]", "", -1) where += "`" + k + "` LIKE ? " v = "%" + ObjToStr(v) res = append(res, v) - case "[~!]"://右边任意 + case "[~!]": //右边任意 k = strings.Replace(k, "[~]", "", -1) where += "`" + k + "` LIKE ? " - v = ObjToStr(v) +"%" + v = ObjToStr(v) + "%" res = append(res, v) - case "[~~]"://手动任意 + case "[~~]": //手动任意 k = strings.Replace(k, "[~]", "", -1) where += "`" + k + "` LIKE ? " //v = ObjToStr(v) @@ -684,9 +677,9 @@ func (this *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { if reflect.ValueOf(v).Type().String() == "hotime.Slice" { where += "`" + k + "` IN (" res = append(res, (v.(Slice))...) - if len(v.(Slice))==0{ - where+=") " - }else{ + if len(v.(Slice)) == 0 { + where += ") " + } else { for i := 0; i < len(v.(Slice)); i++ { if i+1 != len(v.(Slice)) { where += "?," @@ -708,11 +701,11 @@ func (this *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { } else if k == "[#]" { k = strings.Replace(k, "[#]", "", -1) where += " " + ObjToStr(v) + " " - } else { + } else { //fmt.Println(reflect.ValueOf(v).Type().String()) - if v==nil{ + if v == nil { where += "`" + k + "` IS NULL" - }else if reflect.ValueOf(v).Type().String() == "hotime.Slice" { + } else if reflect.ValueOf(v).Type().String() == "hotime.Slice" { //fmt.Println(v) where += "`" + k + "` IN (" @@ -738,8 +731,8 @@ func (this *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { } } else { - where += "`" + k + "`=? " - res = append(res, v) + where += "`" + k + "`=? " + res = append(res, v) } } @@ -751,11 +744,11 @@ func (this *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { func (this *HoTimeDB) notIn(k string, v interface{}, where string, res []interface{}) (string, []interface{}) { //where:="" //fmt.Println(reflect.ValueOf(v).Type().String()) - if v==nil{ + if v == nil { where += "`" + k + "` IS NOT NULL " - }else if reflect.ValueOf(v).Type().String() == "hotime.Slice" { + } else if reflect.ValueOf(v).Type().String() == "hotime.Slice" { where += "`" + k + "` NOT IN (" res = append(res, (v.(Slice))...) for i := 0; i < len(v.(Slice)); i++ { @@ -779,9 +772,8 @@ func (this *HoTimeDB) notIn(k string, v interface{}, where string, res []interfa } } else { - where += "`" + k + "` !=? " - res = append(res, v) - + where += "`" + k + "` !=? " + res = append(res, v) } @@ -861,15 +853,15 @@ func (this *HoTimeDB) Update(table string, data Map, where Map) int64 { res, err := this.Exec(query, qs...) - rows:=int64(0) - if err.GetError() == nil &&res!=nil{ + rows := int64(0) + if err.GetError() == nil && res != nil { rows, _ = res.RowsAffected() } //如果更新成功,则删除缓存 - if rows!=0{ - if this.DBCached&&this.CacheIns!=nil{ - this.Cache(table+"*",nil) + if rows != 0 { + if this.DBCached && this.CacheIns != nil { + this.Cache(table+"*", nil) } } @@ -884,15 +876,15 @@ func (this *HoTimeDB) Delete(table string, data map[string]interface{}) int64 { query += temp res, err := this.Exec(query, resWhere...) - rows:=int64(0) - if err.GetError() == nil &&res!=nil{ + rows := int64(0) + if err.GetError() == nil && res != nil { rows, _ = res.RowsAffected() } //如果删除成功,删除对应缓存 - if rows!=0{ - if this.DBCached&&this.CacheIns!=nil{ - this.Cache(table+"*",nil) + if rows != 0 { + if this.DBCached && this.CacheIns != nil { + this.Cache(table+"*", nil) } } //return 0 @@ -925,16 +917,16 @@ func (this *HoTimeDB) Insert(table string, data map[string]interface{}) int64 { res, err := this.Exec(query, values...) - id:=int64(0) - if err.GetError() == nil &&res!=nil{ + id := int64(0) + if err.GetError() == nil && res != nil { id, this.LastErr.err = res.LastInsertId() } //如果插入成功,删除缓存 - if id!=0{ - if this.DBCached&&this.CacheIns!=nil{ - this.Cache(table+"*",nil) + if id != 0 { + if this.DBCached && this.CacheIns != nil { + this.Cache(table+"*", nil) } }