forked from golang/hotime
62 lines
1006 B
Go
62 lines
1006 B
Go
|
package hotime
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type CacheRedis struct {
|
||
|
Time int64
|
||
|
Map
|
||
|
contextBase
|
||
|
}
|
||
|
|
||
|
//获取Cache键只能为string类型
|
||
|
func (this *CacheRedis) get(key string) interface{} {
|
||
|
this.Error.SetError(nil)
|
||
|
if this.Map[key] == nil {
|
||
|
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 *CacheRedis) 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
|
||
|
|
||
|
if this.Map == nil {
|
||
|
this.Map = Map{}
|
||
|
}
|
||
|
this.Map.Put(key, data)
|
||
|
}
|
||
|
|
||
|
func (this *CacheRedis) delete(key string) {
|
||
|
|
||
|
delete(this.Map, key)
|
||
|
}
|
||
|
|
||
|
//func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
|
||
|
//
|
||
|
//
|
||
|
//
|
||
|
//}
|