缓存测试成功

This commit is contained in:
hoteas 2021-05-28 23:48:33 +08:00
parent 1101937028
commit 339302956a
6 changed files with 40 additions and 35 deletions

View File

@ -180,6 +180,7 @@ func (that *Application) SetCache() {
cacheIns := HoTimeCache{} cacheIns := HoTimeCache{}
cacheIns.Init(that.Config.GetMap("cache"), HoTimeDBInterface(&that.Db), &that.Error) cacheIns.Init(that.Config.GetMap("cache"), HoTimeDBInterface(&that.Db), &that.Error)
that.HoTimeCache = &cacheIns that.HoTimeCache = &cacheIns
that.Db.HoTimeCache = &cacheIns
} }
// SetConfig 设置配置文件路径全路径或者相对路径 // SetConfig 设置配置文件路径全路径或者相对路径

38
cache/cache.go vendored
View File

@ -20,7 +20,7 @@ func (that *HoTimeCache) Session(key string, data ...interface{}) *Obj {
if len(data) == 0 { if len(data) == 0 {
//内存缓存有 //内存缓存有
if that.memoryCache != nil && that.memoryCache.SessionSet { if that.memoryCache != nil && that.memoryCache.SessionSet {
reData = that.memoryCache.Cache(key, data) reData = that.memoryCache.Cache(key, data...)
if reData != nil { if reData != nil {
return reData return reData
} }
@ -28,7 +28,7 @@ func (that *HoTimeCache) Session(key string, data ...interface{}) *Obj {
//redis缓存有 //redis缓存有
if that.redisCache != nil && that.redisCache.SessionSet { if that.redisCache != nil && that.redisCache.SessionSet {
reData = that.redisCache.Cache(key, data) reData = that.redisCache.Cache(key, data...)
if reData != nil { if reData != nil {
if that.memoryCache != nil && that.memoryCache.SessionSet { if that.memoryCache != nil && that.memoryCache.SessionSet {
that.memoryCache.Cache(key, reData) that.memoryCache.Cache(key, reData)
@ -39,7 +39,7 @@ func (that *HoTimeCache) Session(key string, data ...interface{}) *Obj {
//db缓存有 //db缓存有
if that.dbCache != nil && that.dbCache.SessionSet { if that.dbCache != nil && that.dbCache.SessionSet {
reData = that.dbCache.Cache(key, data) reData = that.dbCache.Cache(key, data...)
if reData != nil { if reData != nil {
if that.memoryCache != nil && that.memoryCache.SessionSet { if that.memoryCache != nil && that.memoryCache.SessionSet {
that.memoryCache.Cache(key, reData) 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 { if that.memoryCache != nil && that.memoryCache.SessionSet {
reData = that.memoryCache.Cache(key, data) reData = that.memoryCache.Cache(key, data...)
} }
//redis缓存有 //redis缓存有
if that.redisCache != nil && that.redisCache.SessionSet { if that.redisCache != nil && that.redisCache.SessionSet {
reData = that.redisCache.Cache(key, data) reData = that.redisCache.Cache(key, data...)
} }
//redis缓存有 //redis缓存有
if that.dbCache != nil && that.dbCache.SessionSet { if that.dbCache != nil && that.dbCache.SessionSet {
reData = that.dbCache.Cache(key, data) reData = that.dbCache.Cache(key, data...)
} }
return reData return reData
@ -75,7 +75,7 @@ func (that *HoTimeCache) Db(key string, data ...interface{}) *Obj {
if len(data) == 0 { if len(data) == 0 {
//内存缓存有 //内存缓存有
if that.memoryCache != nil && that.memoryCache.DbSet { if that.memoryCache != nil && that.memoryCache.DbSet {
reData = that.memoryCache.Cache(key, data) reData = that.memoryCache.Cache(key, data...)
if reData != nil { if reData != nil {
return reData return reData
} }
@ -83,7 +83,7 @@ func (that *HoTimeCache) Db(key string, data ...interface{}) *Obj {
//redis缓存有 //redis缓存有
if that.redisCache != nil && that.redisCache.DbSet { if that.redisCache != nil && that.redisCache.DbSet {
reData = that.redisCache.Cache(key, data) reData = that.redisCache.Cache(key, data...)
if reData != nil { if reData != nil {
if that.memoryCache != nil && that.memoryCache.DbSet { if that.memoryCache != nil && that.memoryCache.DbSet {
that.memoryCache.Cache(key, reData) that.memoryCache.Cache(key, reData)
@ -94,7 +94,7 @@ func (that *HoTimeCache) Db(key string, data ...interface{}) *Obj {
//redis缓存有 //redis缓存有
if that.dbCache != nil && that.dbCache.DbSet { if that.dbCache != nil && that.dbCache.DbSet {
reData = that.dbCache.Cache(key, data) reData = that.dbCache.Cache(key, data...)
if reData != nil { if reData != nil {
if that.memoryCache != nil && that.memoryCache.DbSet { if that.memoryCache != nil && that.memoryCache.DbSet {
that.memoryCache.Cache(key, reData) 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 { if that.memoryCache != nil && that.memoryCache.DbSet {
reData = that.memoryCache.Cache(key, data) reData = that.memoryCache.Cache(key, data...)
} }
//redis缓存有 //redis缓存有
if that.redisCache != nil && that.redisCache.DbSet { if that.redisCache != nil && that.redisCache.DbSet {
reData = that.redisCache.Cache(key, data) reData = that.redisCache.Cache(key, data...)
} }
//redis缓存有 //redis缓存有
if that.dbCache != nil && that.dbCache.DbSet { if that.dbCache != nil && that.dbCache.DbSet {
reData = that.dbCache.Cache(key, data) reData = that.dbCache.Cache(key, data...)
} }
return reData return reData
} }
@ -131,7 +131,7 @@ func (that *HoTimeCache) Cache(key string, data ...interface{}) *Obj {
if len(data) == 0 { if len(data) == 0 {
//内存缓存有 //内存缓存有
if that.memoryCache != nil { if that.memoryCache != nil {
reData = that.memoryCache.Cache(key, data) reData = that.memoryCache.Cache(key, data...)
if reData != nil { if reData != nil {
return reData return reData
} }
@ -139,7 +139,7 @@ func (that *HoTimeCache) Cache(key string, data ...interface{}) *Obj {
//redis缓存有 //redis缓存有
if that.redisCache != nil { if that.redisCache != nil {
reData = that.redisCache.Cache(key, data) reData = that.redisCache.Cache(key, data...)
if reData != nil { if reData != nil {
if that.memoryCache != nil { if that.memoryCache != nil {
@ -151,7 +151,7 @@ func (that *HoTimeCache) Cache(key string, data ...interface{}) *Obj {
//redis缓存有 //redis缓存有
if that.dbCache != nil { if that.dbCache != nil {
reData = that.dbCache.Cache(key, data) reData = that.dbCache.Cache(key, data...)
if reData != nil { if reData != nil {
if that.memoryCache != nil { if that.memoryCache != nil {
that.memoryCache.Cache(key, reData) that.memoryCache.Cache(key, reData)
@ -168,15 +168,15 @@ func (that *HoTimeCache) Cache(key string, data ...interface{}) *Obj {
//设置缓存 //设置缓存
//设置内存缓存 //设置内存缓存
if that.memoryCache != nil { if that.memoryCache != nil {
reData = that.memoryCache.Cache(key, data) reData = that.memoryCache.Cache(key, data...)
} }
//redis缓存有 //redis缓存有
if that.redisCache != nil { if that.redisCache != nil {
reData = that.redisCache.Cache(key, data) reData = that.redisCache.Cache(key, data...)
} }
//redis缓存有 //redis缓存有
if that.dbCache != nil { if that.dbCache != nil {
reData = that.dbCache.Cache(key, data) reData = that.dbCache.Cache(key, data...)
} }
return reData return reData
} }
@ -237,7 +237,7 @@ func (that *HoTimeCache) Init(config Map, hotimeDb HoTimeDBInterface, err ...*Er
} }
if redis.Get("timeout") == nil { if redis.Get("timeout") == nil {
redis["timeout"] = 60 * 60 * 24 * 30 redis["timeout"] = 60 * 60 * 24 * 15
} }
that.Config["redis"] = redis that.Config["redis"] = redis
that.redisCache = &CacheRedis{TimeOut: redis.GetCeilInt64("timeout"), that.redisCache = &CacheRedis{TimeOut: redis.GetCeilInt64("timeout"),

5
cache/cache_db.go vendored
View File

@ -23,7 +23,6 @@ type CacheDb struct {
TimeOut int64 TimeOut int64
DbSet bool DbSet bool
SessionSet bool SessionSet bool
Time int64
Db HoTimeDBInterface Db HoTimeDBInterface
*Error *Error
ContextBase ContextBase
@ -152,10 +151,10 @@ func (that *CacheDb) Cache(key string, data ...interface{}) *Obj {
} }
if len(data) == 1 { if len(data) == 1 {
if that.Time == 0 { if that.TimeOut == 0 {
//that.Time = Config.GetInt64("cacheLongTime") //that.Time = Config.GetInt64("cacheLongTime")
} }
tim += that.Time tim += that.TimeOut
} }
if len(data) == 2 { if len(data) == 2 {
that.SetError(nil) that.SetError(nil)

View File

@ -48,12 +48,15 @@ func (this *CacheRedis) reCon() bool {
return false return false
} }
if this.Pwd != "" {
_, err = this.conn.Do("AUTH", this.Pwd) _, err = this.conn.Do("AUTH", this.Pwd)
if err != nil { if err != nil {
this.conn = nil this.conn = nil
this.Error.SetError(err) this.Error.SetError(err)
return false return false
} }
}
return true return true
} }
func (this *CacheRedis) del(key string) { func (this *CacheRedis) del(key string) {

View File

@ -467,7 +467,7 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
qs = append(qs, resWhere...) qs = append(qs, resWhere...)
md5 := that.md5(query, qs...) md5 := that.md5(query, qs...)
if that.HoTimeCache != nil { if that.HoTimeCache != nil && table != "cached" {
//如果缓存有则从缓存取 //如果缓存有则从缓存取
cacheData := that.HoTimeCache.Db(table + ":" + md5) 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) _ = 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 rows != 0 {
if that.HoTimeCache != nil { if that.HoTimeCache != nil && table != "cached" {
_ = that.HoTimeCache.Db(table+"*", nil) _ = that.HoTimeCache.Db(table+"*", nil)
} }
} }
@ -911,7 +911,7 @@ func (that *HoTimeDB) Delete(table string, data map[string]interface{}) int64 {
//如果删除成功,删除对应缓存 //如果删除成功,删除对应缓存
if rows != 0 { if rows != 0 {
if that.HoTimeCache != nil { if that.HoTimeCache != nil && table != "cached" {
_ = that.HoTimeCache.Db(table+"*", nil) _ = that.HoTimeCache.Db(table+"*", nil)
} }
} }
@ -953,7 +953,7 @@ func (that *HoTimeDB) Insert(table string, data map[string]interface{}) int64 {
//如果插入成功,删除缓存 //如果插入成功,删除缓存
if id != 0 { if id != 0 {
if that.HoTimeCache != nil { if that.HoTimeCache != nil && table != "cached" {
_ = that.HoTimeCache.Db(table+"*", nil) _ = that.HoTimeCache.Db(table+"*", nil)
} }
} }

View File

@ -49,9 +49,11 @@ func main() {
"app": hotime.Proj{ "app": hotime.Proj{
"index": hotime.Ctr{ "index": hotime.Ctr{
"test": func(this *hotime.Context) { "test": func(this *hotime.Context) {
fmt.Println(this.Session("test").Data) data := this.Db.Get("cached", "*")
this.Session("test", 145484978484) fmt.Println(data)
fmt.Println(this.Session("test").Data) fmt.Println(this.Session("test").ToCeilInt())
this.Session("test1", 98984984)
fmt.Println(this.Session("test1").Data)
//fmt.Println(this.Db.GetTag()) //fmt.Println(this.Db.GetTag())
//this.Application.Log.Error("dasdasdas") //this.Application.Log.Error("dasdasdas")
//this.Log.Error("dadasdasd") //this.Log.Error("dadasdasd")