hotime/cache_redis.go

60 lines
868 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 19:16:11 +00:00
"github.com/garyburd/redigo/redis"
2018-04-03 17:54:27 +00:00
)
type CacheRedis struct {
Time int64
2018-04-03 19:16:11 +00:00
redis.Conn
2018-04-03 17:54:27 +00:00
contextBase
}
2018-04-03 19:11:51 +00:00
func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
2018-04-03 19:16:11 +00:00
redis.Dial()
2018-04-03 19:11:51 +00:00
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
}