This commit is contained in:
hoteas 2018-06-13 11:10:13 +00:00
parent 9ff8f50fb9
commit 3f0780e6ea
3 changed files with 62 additions and 60 deletions

View File

@ -138,10 +138,12 @@ func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
if len(data) == 2 { if len(data) == 2 {
this.Error.SetError(nil) this.Error.SetError(nil)
tempt := ObjToInt64(data[1], &this.Error) tempt := ObjToInt64(data[1], &this.Error)
if this.GetError() == nil { if tempt > tim {
tim = tempt
} else if this.GetError() == nil {
tim = tim + tempt tim = tim + tempt
} }
} }

View File

@ -106,10 +106,11 @@ func (this *CacheDb) Cache(key string, data ...interface{}) *Obj {
this.SetError(nil) this.SetError(nil)
tempt := ObjToInt64(data[1], &this.Error) tempt := ObjToInt64(data[1], &this.Error)
if this.GetError() == nil { if tempt > tim {
tim = tempt
} else if this.GetError() == nil {
tim = tim + tempt tim = tim + tempt
} }
} }
this.set(key, data[0], tim) this.set(key, data[0], tim)

View File

@ -1,9 +1,9 @@
package hotime package hotime
import ( import (
"time"
"sync"
"strings" "strings"
"sync"
"time"
) )
type CacheMemory struct { type CacheMemory struct {
@ -24,20 +24,20 @@ func (this *CacheMemory) get(key string) interface{} {
return nil return nil
} }
//data:=cacheMap[key]; //data:=cacheMap[key];
if data.time <= time.Now().Unix() { if data.time < time.Now().Unix() {
delete(this.Map, key) delete(this.Map, key)
return nil return nil
} }
return data.data return data.data
} }
func (this *CacheMemory)refreshMap(){ func (this *CacheMemory) refreshMap() {
go func() { go func() {
this.mutex.Lock() this.mutex.Lock()
defer this.mutex.Unlock() defer this.mutex.Unlock()
for key,v:=range this.Map{ for key, v := range this.Map {
data:=v.(cacheData) data := v.(cacheData)
if data.time <= time.Now().Unix() { if data.time <= time.Now().Unix() {
delete(this.Map, key) delete(this.Map, key)
} }
@ -45,8 +45,6 @@ func (this *CacheMemory)refreshMap(){
}() }()
} }
//key value ,时间为时间戳 //key value ,时间为时间戳
@ -71,17 +69,17 @@ func (this *CacheMemory) set(key string, value interface{}, time int64) {
} }
func (this *CacheMemory) delete(key string) { func (this *CacheMemory) delete(key string) {
del:=strings.Index(key,"*") del := strings.Index(key, "*")
//如果通配删除 //如果通配删除
if del!=-1{ if del != -1 {
key=Substr(key,0,del) key = Substr(key, 0, del)
for k,_:=range this.Map{ for k, _ := range this.Map {
if strings.Index(k,key)!=-1{ if strings.Index(k, key) != -1 {
delete(this.Map, key) delete(this.Map, key)
} }
} }
}else{ } else {
delete(this.Map, key) delete(this.Map, key)
} }
@ -89,21 +87,19 @@ func (this *CacheMemory) delete(key string) {
func (this *CacheMemory) Cache(key string, data ...interface{}) *Obj { func (this *CacheMemory) Cache(key string, data ...interface{}) *Obj {
x := RandX(1, 100000)
x:=RandX(1,100000) if x > 99950 {
if x>99950{
this.refreshMap() this.refreshMap()
} }
if this.mutex==nil{ if this.mutex == nil {
this.mutex=&sync.RWMutex{} this.mutex = &sync.RWMutex{}
} }
reData:= &Obj{} reData := &Obj{Data: nil}
if len(data) == 0 { if len(data) == 0 {
this.mutex.RLock() this.mutex.RLock()
reData.Data=this.get(key) reData.Data = this.get(key)
this.mutex.RUnlock() this.mutex.RUnlock()
return reData return reData
} }
@ -122,15 +118,18 @@ func (this *CacheMemory) Cache(key string, data ...interface{}) *Obj {
this.Time = Config.GetInt64("cacheShortTime") this.Time = Config.GetInt64("cacheShortTime")
} }
tim += this.Time tim = tim + this.Time
} }
if len(data) == 2 { if len(data) == 2 {
this.Error.SetError(nil) this.Error.SetError(nil)
tempt := ObjToInt64(data[1], &this.Error) tempt := ObjToInt64(data[1], &this.Error)
if this.GetError() == nil {
if tempt > tim {
tim = tempt
} else if this.GetError() == nil {
tim = tim + tempt tim = tim + tempt
} }
} }
this.mutex.Lock() this.mutex.Lock()