From 339302956ac4774da5bf608930dc4c35218ab87e Mon Sep 17 00:00:00 2001 From: hoteas Date: Fri, 28 May 2021 23:48:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=B5=8B=E8=AF=95=E6=88=90?= =?UTF-8?q?=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application.go | 1 + cache/cache.go | 38 +++++++++++++++++++------------------- cache/cache_db.go | 5 ++--- cache/cache_redis.go | 13 ++++++++----- db/db.go | 10 +++++----- example/main.go | 8 +++++--- 6 files changed, 40 insertions(+), 35 deletions(-) diff --git a/application.go b/application.go index 18706e6..3270015 100644 --- a/application.go +++ b/application.go @@ -180,6 +180,7 @@ func (that *Application) SetCache() { cacheIns := HoTimeCache{} cacheIns.Init(that.Config.GetMap("cache"), HoTimeDBInterface(&that.Db), &that.Error) that.HoTimeCache = &cacheIns + that.Db.HoTimeCache = &cacheIns } // SetConfig 设置配置文件路径全路径或者相对路径 diff --git a/cache/cache.go b/cache/cache.go index 59500e8..3f5c737 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -20,7 +20,7 @@ func (that *HoTimeCache) Session(key string, data ...interface{}) *Obj { if len(data) == 0 { //内存缓存有 if that.memoryCache != nil && that.memoryCache.SessionSet { - reData = that.memoryCache.Cache(key, data) + reData = that.memoryCache.Cache(key, data...) if reData != nil { return reData } @@ -28,7 +28,7 @@ func (that *HoTimeCache) Session(key string, data ...interface{}) *Obj { //redis缓存有 if that.redisCache != nil && that.redisCache.SessionSet { - reData = that.redisCache.Cache(key, data) + reData = that.redisCache.Cache(key, data...) if reData != nil { if that.memoryCache != nil && that.memoryCache.SessionSet { that.memoryCache.Cache(key, reData) @@ -39,7 +39,7 @@ func (that *HoTimeCache) Session(key string, data ...interface{}) *Obj { //db缓存有 if that.dbCache != nil && that.dbCache.SessionSet { - reData = that.dbCache.Cache(key, data) + reData = that.dbCache.Cache(key, data...) if reData != nil { if that.memoryCache != nil && that.memoryCache.SessionSet { that.memoryCache.Cache(key, reData) @@ -57,15 +57,15 @@ func (that *HoTimeCache) Session(key string, data ...interface{}) *Obj { //设置缓存 //设置内存缓存 if that.memoryCache != nil && that.memoryCache.SessionSet { - reData = that.memoryCache.Cache(key, data) + reData = that.memoryCache.Cache(key, data...) } //redis缓存有 if that.redisCache != nil && that.redisCache.SessionSet { - reData = that.redisCache.Cache(key, data) + reData = that.redisCache.Cache(key, data...) } //redis缓存有 if that.dbCache != nil && that.dbCache.SessionSet { - reData = that.dbCache.Cache(key, data) + reData = that.dbCache.Cache(key, data...) } return reData @@ -75,7 +75,7 @@ func (that *HoTimeCache) Db(key string, data ...interface{}) *Obj { if len(data) == 0 { //内存缓存有 if that.memoryCache != nil && that.memoryCache.DbSet { - reData = that.memoryCache.Cache(key, data) + reData = that.memoryCache.Cache(key, data...) if reData != nil { return reData } @@ -83,7 +83,7 @@ func (that *HoTimeCache) Db(key string, data ...interface{}) *Obj { //redis缓存有 if that.redisCache != nil && that.redisCache.DbSet { - reData = that.redisCache.Cache(key, data) + reData = that.redisCache.Cache(key, data...) if reData != nil { if that.memoryCache != nil && that.memoryCache.DbSet { that.memoryCache.Cache(key, reData) @@ -94,7 +94,7 @@ func (that *HoTimeCache) Db(key string, data ...interface{}) *Obj { //redis缓存有 if that.dbCache != nil && that.dbCache.DbSet { - reData = that.dbCache.Cache(key, data) + reData = that.dbCache.Cache(key, data...) if reData != nil { if that.memoryCache != nil && that.memoryCache.DbSet { that.memoryCache.Cache(key, reData) @@ -113,15 +113,15 @@ func (that *HoTimeCache) Db(key string, data ...interface{}) *Obj { //设置缓存 //设置内存缓存 if that.memoryCache != nil && that.memoryCache.DbSet { - reData = that.memoryCache.Cache(key, data) + reData = that.memoryCache.Cache(key, data...) } //redis缓存有 if that.redisCache != nil && that.redisCache.DbSet { - reData = that.redisCache.Cache(key, data) + reData = that.redisCache.Cache(key, data...) } //redis缓存有 if that.dbCache != nil && that.dbCache.DbSet { - reData = that.dbCache.Cache(key, data) + reData = that.dbCache.Cache(key, data...) } return reData } @@ -131,7 +131,7 @@ func (that *HoTimeCache) Cache(key string, data ...interface{}) *Obj { if len(data) == 0 { //内存缓存有 if that.memoryCache != nil { - reData = that.memoryCache.Cache(key, data) + reData = that.memoryCache.Cache(key, data...) if reData != nil { return reData } @@ -139,7 +139,7 @@ func (that *HoTimeCache) Cache(key string, data ...interface{}) *Obj { //redis缓存有 if that.redisCache != nil { - reData = that.redisCache.Cache(key, data) + reData = that.redisCache.Cache(key, data...) if reData != nil { if that.memoryCache != nil { @@ -151,7 +151,7 @@ func (that *HoTimeCache) Cache(key string, data ...interface{}) *Obj { //redis缓存有 if that.dbCache != nil { - reData = that.dbCache.Cache(key, data) + reData = that.dbCache.Cache(key, data...) if reData != nil { if that.memoryCache != nil { that.memoryCache.Cache(key, reData) @@ -168,15 +168,15 @@ func (that *HoTimeCache) Cache(key string, data ...interface{}) *Obj { //设置缓存 //设置内存缓存 if that.memoryCache != nil { - reData = that.memoryCache.Cache(key, data) + reData = that.memoryCache.Cache(key, data...) } //redis缓存有 if that.redisCache != nil { - reData = that.redisCache.Cache(key, data) + reData = that.redisCache.Cache(key, data...) } //redis缓存有 if that.dbCache != nil { - reData = that.dbCache.Cache(key, data) + reData = that.dbCache.Cache(key, data...) } return reData } @@ -237,7 +237,7 @@ func (that *HoTimeCache) Init(config Map, hotimeDb HoTimeDBInterface, err ...*Er } if redis.Get("timeout") == nil { - redis["timeout"] = 60 * 60 * 24 * 30 + redis["timeout"] = 60 * 60 * 24 * 15 } that.Config["redis"] = redis that.redisCache = &CacheRedis{TimeOut: redis.GetCeilInt64("timeout"), diff --git a/cache/cache_db.go b/cache/cache_db.go index 9d6d05c..4fbe234 100644 --- a/cache/cache_db.go +++ b/cache/cache_db.go @@ -23,7 +23,6 @@ type CacheDb struct { TimeOut int64 DbSet bool SessionSet bool - Time int64 Db HoTimeDBInterface *Error ContextBase @@ -152,10 +151,10 @@ func (that *CacheDb) Cache(key string, data ...interface{}) *Obj { } if len(data) == 1 { - if that.Time == 0 { + if that.TimeOut == 0 { //that.Time = Config.GetInt64("cacheLongTime") } - tim += that.Time + tim += that.TimeOut } if len(data) == 2 { that.SetError(nil) diff --git a/cache/cache_redis.go b/cache/cache_redis.go index fc4ca1b..5ee4d92 100644 --- a/cache/cache_redis.go +++ b/cache/cache_redis.go @@ -48,12 +48,15 @@ func (this *CacheRedis) reCon() bool { return false } - _, err = this.conn.Do("AUTH", this.Pwd) - if err != nil { - this.conn = nil - this.Error.SetError(err) - return false + if this.Pwd != "" { + _, err = this.conn.Do("AUTH", this.Pwd) + if err != nil { + this.conn = nil + this.Error.SetError(err) + return false + } } + return true } func (this *CacheRedis) del(key string) { diff --git a/db/db.go b/db/db.go index 7d07e53..e6de390 100644 --- a/db/db.go +++ b/db/db.go @@ -467,7 +467,7 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map { qs = append(qs, resWhere...) md5 := that.md5(query, qs...) - if that.HoTimeCache != nil { + if that.HoTimeCache != nil && table != "cached" { //如果缓存有则从缓存取 cacheData := that.HoTimeCache.Db(table + ":" + md5) @@ -484,7 +484,7 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map { } //缓存 - if that.HoTimeCache != nil { + if that.HoTimeCache != nil && table != "cached" { _ = that.HoTimeCache.Db(table+":"+md5, res) } @@ -888,7 +888,7 @@ func (that *HoTimeDB) Update(table string, data Map, where Map) int64 { //如果更新成功,则删除缓存 if rows != 0 { - if that.HoTimeCache != nil { + if that.HoTimeCache != nil && table != "cached" { _ = that.HoTimeCache.Db(table+"*", nil) } } @@ -911,7 +911,7 @@ func (that *HoTimeDB) Delete(table string, data map[string]interface{}) int64 { //如果删除成功,删除对应缓存 if rows != 0 { - if that.HoTimeCache != nil { + if that.HoTimeCache != nil && table != "cached" { _ = that.HoTimeCache.Db(table+"*", nil) } } @@ -953,7 +953,7 @@ func (that *HoTimeDB) Insert(table string, data map[string]interface{}) int64 { //如果插入成功,删除缓存 if id != 0 { - if that.HoTimeCache != nil { + if that.HoTimeCache != nil && table != "cached" { _ = that.HoTimeCache.Db(table+"*", nil) } } diff --git a/example/main.go b/example/main.go index 1d73fef..a601e37 100644 --- a/example/main.go +++ b/example/main.go @@ -49,9 +49,11 @@ func main() { "app": hotime.Proj{ "index": hotime.Ctr{ "test": func(this *hotime.Context) { - fmt.Println(this.Session("test").Data) - this.Session("test", 145484978484) - fmt.Println(this.Session("test").Data) + data := this.Db.Get("cached", "*") + fmt.Println(data) + fmt.Println(this.Session("test").ToCeilInt()) + this.Session("test1", 98984984) + fmt.Println(this.Session("test1").Data) //fmt.Println(this.Db.GetTag()) //this.Application.Log.Error("dasdasdas") //this.Log.Error("dadasdasd")