forked from golang/hotime
59 lines
812 B
Go
59 lines
812 B
Go
package hotime
|
|
|
|
import (
|
|
"time"
|
|
"sync"
|
|
)
|
|
|
|
type CacheRedis struct {
|
|
Time int64
|
|
Map
|
|
contextBase
|
|
}
|
|
|
|
func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
|
|
|
|
|
|
|
|
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 {
|
|
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.set(key, data[0], tim)
|
|
this.mutex.Unlock()
|
|
return reData
|
|
|
|
}
|