数据库缓存

This commit is contained in:
2019-11-10 18:00:45 +08:00
parent 7d479f181a
commit 048c457303
4 changed files with 127 additions and 104 deletions
+27 -35
View File
@@ -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 {