2017-08-04 08:20:59 +00:00
|
|
|
package hotime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CacheMemory struct {
|
|
|
|
Time int64
|
|
|
|
Map
|
2017-08-23 07:35:49 +00:00
|
|
|
contextBase
|
2017-08-04 08:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//获取Cache键只能为string类型
|
|
|
|
func (this *CacheMemory) get(key string) interface{} {
|
|
|
|
this.Error.SetError(nil)
|
2017-08-23 02:05:47 +00:00
|
|
|
if this.Map[key] == nil {
|
2017-08-04 08:20:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
data := this.Map.Get(key, &this.Error).(cacheData)
|
|
|
|
if this.GetError() != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
//data:=cacheMap[key];
|
|
|
|
if data.time <= time.Now().Unix() {
|
|
|
|
delete(this.Map, key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return data.data
|
|
|
|
}
|
|
|
|
|
|
|
|
//key value ,时间为时间戳
|
|
|
|
func (this *CacheMemory) set(key string, value interface{}, time int64) {
|
|
|
|
this.Error.SetError(nil)
|
|
|
|
var data cacheData
|
|
|
|
dd := this.Map.Get(key, &this.Error)
|
|
|
|
|
|
|
|
if dd == nil {
|
|
|
|
data = cacheData{}
|
|
|
|
} else {
|
|
|
|
data = dd.(cacheData)
|
|
|
|
}
|
|
|
|
|
|
|
|
data.time = time
|
|
|
|
data.data = value
|
|
|
|
|
2017-08-23 02:05:47 +00:00
|
|
|
if this.Map == nil {
|
|
|
|
this.Map = Map{}
|
2017-08-04 08:20:59 +00:00
|
|
|
}
|
2017-08-23 02:05:47 +00:00
|
|
|
this.Map.Put(key, data)
|
2017-08-04 08:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *CacheMemory) delete(key string) {
|
|
|
|
|
|
|
|
delete(this.Map, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *CacheMemory) Cache(key string, data ...interface{}) *Obj {
|
|
|
|
|
|
|
|
return SafeMutex(MUTEX_MEMORY_CACHE, func() interface{} {
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return &Obj{Data: this.get(key)}
|
|
|
|
}
|
|
|
|
tim := time.Now().Unix()
|
|
|
|
|
|
|
|
if len(data) == 1 && data[0] == nil {
|
|
|
|
this.delete(key)
|
|
|
|
return &Obj{Data: nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.set(key, data[0], tim)
|
|
|
|
return &Obj{Data: nil}
|
|
|
|
}).(*Obj)
|
|
|
|
|
|
|
|
}
|