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,53 +87,54 @@ 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)
if x > 99950 {
this.refreshMap()
}
if this.mutex == nil {
this.mutex = &sync.RWMutex{}
}
x:=RandX(1,100000) reData := &Obj{Data: nil}
if x>99950{
this.refreshMap()
}
if this.mutex==nil{
this.mutex=&sync.RWMutex{}
}
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) == 1 && data[0] == nil {
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
}
}
this.mutex.Lock() this.mutex.Lock()
this.set(key, data[0], tim) this.delete(key)
this.mutex.Unlock() this.mutex.Unlock()
return reData 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
} }