iedc-go/cache_redis.go

59 lines
812 B
Go
Raw Normal View History

2018-04-03 17:54:27 +00:00
package hotime
import (
"time"
2018-04-03 19:11:51 +00:00
"sync"
2018-04-03 17:54:27 +00:00
)
type CacheRedis struct {
Time int64
Map
contextBase
}
2018-04-03 19:11:51 +00:00
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
2018-04-03 17:54:27 +00:00
}
2018-04-03 19:11:51 +00:00
tim := time.Now().Unix()
if len(data) == 1 && data[0] == nil {
this.mutex.Lock()
this.delete(key)
this.mutex.Unlock()
return reData
2018-04-03 17:54:27 +00:00
}
2018-04-03 19:11:51 +00:00
if len(data) == 1 {
if this.Time == 0 {
this.Time = Config.GetInt64("cacheShortTime")
}
2018-04-03 17:54:27 +00:00
2018-04-03 19:11:51 +00:00
tim += this.Time
2018-04-03 17:54:27 +00:00
}
2018-04-03 19:11:51 +00:00
if len(data) == 2 {
this.Error.SetError(nil)
tempt := ObjToInt64(data[1], &this.Error)
if this.GetError() == nil {
2018-04-03 17:54:27 +00:00
2018-04-03 19:11:51 +00:00
tim = tim + tempt
2018-04-03 17:54:27 +00:00
2018-04-03 19:11:51 +00:00
}
2018-04-03 17:54:27 +00:00
}
2018-04-03 19:11:51 +00:00
this.mutex.Lock()
this.set(key, data[0], tim)
this.mutex.Unlock()
return reData
2018-04-03 17:54:27 +00:00
}