From 3f0780e6ea0decf332e27a4d2605a5454e81b8ea Mon Sep 17 00:00:00 2001 From: hoteas Date: Wed, 13 Jun 2018 11:10:13 +0000 Subject: [PATCH] --- cache/cache_redis.go | 6 ++- cache_db.go | 5 +- cache_memory.go | 111 +++++++++++++++++++++---------------------- 3 files changed, 62 insertions(+), 60 deletions(-) diff --git a/cache/cache_redis.go b/cache/cache_redis.go index e40254a..04536a6 100644 --- a/cache/cache_redis.go +++ b/cache/cache_redis.go @@ -138,10 +138,12 @@ func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj { if len(data) == 2 { this.Error.SetError(nil) tempt := ObjToInt64(data[1], &this.Error) - if this.GetError() == nil { + if tempt > tim { + + tim = tempt + } else if this.GetError() == nil { tim = tim + tempt - } } diff --git a/cache_db.go b/cache_db.go index 53c0661..1752cae 100644 --- a/cache_db.go +++ b/cache_db.go @@ -106,10 +106,11 @@ func (this *CacheDb) Cache(key string, data ...interface{}) *Obj { this.SetError(nil) tempt := ObjToInt64(data[1], &this.Error) - if this.GetError() == nil { + if tempt > tim { + tim = tempt + } else if this.GetError() == nil { tim = tim + tempt - } } this.set(key, data[0], tim) diff --git a/cache_memory.go b/cache_memory.go index e8f2d99..9892b50 100644 --- a/cache_memory.go +++ b/cache_memory.go @@ -1,9 +1,9 @@ package hotime import ( - "time" - "sync" "strings" + "sync" + "time" ) type CacheMemory struct { @@ -24,20 +24,20 @@ func (this *CacheMemory) get(key string) interface{} { return nil } //data:=cacheMap[key]; - if data.time <= time.Now().Unix() { + if data.time < time.Now().Unix() { delete(this.Map, key) return nil } return data.data } -func (this *CacheMemory)refreshMap(){ +func (this *CacheMemory) refreshMap() { go func() { this.mutex.Lock() defer this.mutex.Unlock() - for key,v:=range this.Map{ - data:=v.(cacheData) + for key, v := range this.Map { + data := v.(cacheData) if data.time <= time.Now().Unix() { delete(this.Map, key) } @@ -45,8 +45,6 @@ func (this *CacheMemory)refreshMap(){ }() - - } //key value ,时间为时间戳 @@ -71,17 +69,17 @@ func (this *CacheMemory) set(key string, value interface{}, time int64) { } func (this *CacheMemory) delete(key string) { - del:=strings.Index(key,"*") + del := strings.Index(key, "*") //如果通配删除 - if del!=-1{ - key=Substr(key,0,del) - for k,_:=range this.Map{ - if strings.Index(k,key)!=-1{ + if del != -1 { + key = Substr(key, 0, del) + for k, _ := range this.Map { + if strings.Index(k, key) != -1 { delete(this.Map, key) } } - }else{ + } else { delete(this.Map, key) } @@ -89,53 +87,54 @@ func (this *CacheMemory) delete(key string) { func (this *CacheMemory) Cache(key string, data ...interface{}) *Obj { + x := RandX(1, 100000) + if x > 99950 { + this.refreshMap() + } + if this.mutex == nil { + this.mutex = &sync.RWMutex{} + } - x:=RandX(1,100000) - if x>99950{ - this.refreshMap() - } - if this.mutex==nil{ - this.mutex=&sync.RWMutex{} - } + reData := &Obj{Data: nil} - reData:= &Obj{} + if len(data) == 0 { + this.mutex.RLock() + reData.Data = this.get(key) + this.mutex.RUnlock() + return reData + } + tim := time.Now().Unix() - - if len(data) == 0 { - this.mutex.RLock() - reData.Data=this.get(key) - this.mutex.RUnlock() - return reData - } - tim := time.Now().Unix() - - if len(data) == 1 && data[0] == nil { - this.mutex.Lock() - this.delete(key) - this.mutex.Unlock() - return reData - } - - if len(data) == 1 { - - if this.Time == 0 { - this.Time = Config.GetInt64("cacheShortTime") - } - - tim += this.Time - } - if len(data) == 2 { - this.Error.SetError(nil) - tempt := ObjToInt64(data[1], &this.Error) - if this.GetError() == nil { - - tim = tim + tempt - - } - } + if len(data) == 1 && data[0] == nil { this.mutex.Lock() - this.set(key, data[0], tim) + this.delete(key) this.mutex.Unlock() return reData + } + + if len(data) == 1 { + + if this.Time == 0 { + this.Time = Config.GetInt64("cacheShortTime") + } + + tim = tim + this.Time + } + if len(data) == 2 { + this.Error.SetError(nil) + tempt := ObjToInt64(data[1], &this.Error) + + if tempt > tim { + + tim = tempt + } else if this.GetError() == nil { + + tim = tim + tempt + } + } + this.mutex.Lock() + this.set(key, data[0], tim) + this.mutex.Unlock() + return reData }