diff --git a/application.go b/application.go index acc140d..f3c1a89 100644 --- a/application.go +++ b/application.go @@ -133,8 +133,20 @@ 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 + } + } //设置配置文件路径全路径或者相对路径 diff --git a/db.go b/db.go index 6d7f5f6..fab4b98 100644 --- a/db.go +++ b/db.go @@ -13,6 +13,7 @@ type HoTimeDB struct { *sql.DB contextBase CacheIns + DBCached bool LastQuery string LastData []interface{} ConnectFunc func(err ...*Error) *sql.DB @@ -30,7 +31,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} + db := HoTimeDB{DB: this.DB,CacheIns:this.CacheIns,DBCached:this.DBCached} tx, err := db.Begin() if err != nil { this.LastErr.SetError(err) @@ -427,7 +428,7 @@ func (this *HoTimeDB) Select(table string, qu ...interface{}) []Map { qs = append(qs, resWhere...) md5:=this.md5(query,qs...) - if this.CacheIns!=nil{ + if this.DBCached&&this.CacheIns!=nil{ //如果缓存有则从缓存取 cacheData:=this.Cache(table+":"+md5) @@ -448,7 +449,7 @@ func (this *HoTimeDB) Select(table string, qu ...interface{}) []Map { } //缓存 - if this.CacheIns!=nil{ + if this.DBCached&&this.CacheIns!=nil{ this.Cache(table+":"+md5,res) } @@ -854,7 +855,7 @@ func (this *HoTimeDB) Update(table string, data Map, where Map) int64 { //如果更新成功,则删除缓存 if rows!=0{ - if this.CacheIns!=nil{ + if this.DBCached&&this.CacheIns!=nil{ this.Cache(table+"*",nil) } } @@ -877,7 +878,7 @@ func (this *HoTimeDB) Delete(table string, data map[string]interface{}) int64 { //如果删除成功,删除对应缓存 if rows!=0{ - if this.CacheIns!=nil{ + if this.DBCached&&this.CacheIns!=nil{ this.Cache(table+"*",nil) } } @@ -919,7 +920,7 @@ func (this *HoTimeDB) Insert(table string, data map[string]interface{}) int64 { //如果插入成功,删除缓存 if id!=0{ - if this.CacheIns!=nil{ + if this.DBCached&&this.CacheIns!=nil{ this.Cache(table+"*",nil) } } diff --git a/var.go b/var.go index 08bb0a1..66cbf9b 100644 --- a/var.go +++ b/var.go @@ -16,6 +16,7 @@ var Config = Map{ "dbUser": "root", "dbPwd": "root", "dbPort": "3306", + "dbCached":0,//0不开启缓存 "port": "0", "cacheShortTime": 60 * 60 * 2, "cacheLongTime": 60 * 60 * 24 * 30,