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 {
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
}
}

View File

@ -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)

View File

@ -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
}