数据库缓存增加设置

This commit is contained in:
hoteas 2019-05-27 17:01:13 +00:00
parent 374e0ab2ac
commit 060b74f94c
3 changed files with 20 additions and 6 deletions

View File

@ -133,8 +133,20 @@ func (this *Application) Run(router Router) {
//启动实例 //启动实例
func (this *Application) SetConnectDB(connect func(err ...*Error) *sql.DB) { 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.connectDbFunc = connect
this.Db.SetConnect(this.connectDbFunc) this.Db.SetConnect(this.connectDbFunc)
this.Db.DBCached=false
if this.Config.GetCeilInt("dbCached")!=0{
this.Db.DBCached=true
}
} }
//设置配置文件路径全路径或者相对路径 //设置配置文件路径全路径或者相对路径

13
db.go
View File

@ -13,6 +13,7 @@ type HoTimeDB struct {
*sql.DB *sql.DB
contextBase contextBase
CacheIns CacheIns
DBCached bool
LastQuery string LastQuery string
LastData []interface{} LastData []interface{}
ConnectFunc func(err ...*Error) *sql.DB 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则回滚 //事务如果action返回true则执行成功false则回滚
func (this *HoTimeDB) Action(action func(db HoTimeDB) bool) bool { 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() tx, err := db.Begin()
if err != nil { if err != nil {
this.LastErr.SetError(err) this.LastErr.SetError(err)
@ -427,7 +428,7 @@ func (this *HoTimeDB) Select(table string, qu ...interface{}) []Map {
qs = append(qs, resWhere...) qs = append(qs, resWhere...)
md5:=this.md5(query,qs...) md5:=this.md5(query,qs...)
if this.CacheIns!=nil{ if this.DBCached&&this.CacheIns!=nil{
//如果缓存有则从缓存取 //如果缓存有则从缓存取
cacheData:=this.Cache(table+":"+md5) 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) this.Cache(table+":"+md5,res)
} }
@ -854,7 +855,7 @@ func (this *HoTimeDB) Update(table string, data Map, where Map) int64 {
//如果更新成功,则删除缓存 //如果更新成功,则删除缓存
if rows!=0{ if rows!=0{
if this.CacheIns!=nil{ if this.DBCached&&this.CacheIns!=nil{
this.Cache(table+"*",nil) this.Cache(table+"*",nil)
} }
} }
@ -877,7 +878,7 @@ func (this *HoTimeDB) Delete(table string, data map[string]interface{}) int64 {
//如果删除成功,删除对应缓存 //如果删除成功,删除对应缓存
if rows!=0{ if rows!=0{
if this.CacheIns!=nil{ if this.DBCached&&this.CacheIns!=nil{
this.Cache(table+"*",nil) this.Cache(table+"*",nil)
} }
} }
@ -919,7 +920,7 @@ func (this *HoTimeDB) Insert(table string, data map[string]interface{}) int64 {
//如果插入成功,删除缓存 //如果插入成功,删除缓存
if id!=0{ if id!=0{
if this.CacheIns!=nil{ if this.DBCached&&this.CacheIns!=nil{
this.Cache(table+"*",nil) this.Cache(table+"*",nil)
} }
} }

1
var.go
View File

@ -16,6 +16,7 @@ var Config = Map{
"dbUser": "root", "dbUser": "root",
"dbPwd": "root", "dbPwd": "root",
"dbPort": "3306", "dbPort": "3306",
"dbCached":0,//0不开启缓存
"port": "0", "port": "0",
"cacheShortTime": 60 * 60 * 2, "cacheShortTime": 60 * 60 * 2,
"cacheLongTime": 60 * 60 * 24 * 30, "cacheLongTime": 60 * 60 * 24 * 30,