hotime/cache/cache_redis.go

171 lines
3.0 KiB
Go
Raw Normal View History

2021-05-23 23:27:41 +00:00
package cache
2018-04-04 18:44:00 +00:00
import (
2022-03-12 17:12:29 +00:00
. "code.hoteas.com/golang/hotime/common"
2018-04-04 18:44:00 +00:00
"github.com/garyburd/redigo/redis"
"strings"
"time"
2018-04-04 18:44:00 +00:00
)
type CacheRedis struct {
TimeOut int64
DbSet bool
SessionSet bool
Host string
Pwd string
Port int64
conn redis.Conn
tag int64
2021-05-25 12:27:24 +00:00
ContextBase
*Error
}
2022-03-12 17:48:54 +00:00
func (that *CacheRedis) GetError() *Error {
2022-03-12 17:48:54 +00:00
return that.Error
}
2022-03-12 17:48:54 +00:00
func (that *CacheRedis) SetError(err *Error) {
that.Error = err
2018-04-04 18:44:00 +00:00
}
//唯一标志
2022-03-12 17:48:54 +00:00
func (that *CacheRedis) GetTag() int64 {
2018-04-04 18:44:00 +00:00
2022-03-12 17:48:54 +00:00
if that.tag == int64(0) {
that.tag = time.Now().UnixNano()
2018-04-04 18:44:00 +00:00
}
2022-03-12 17:48:54 +00:00
return that.tag
2018-04-04 18:44:00 +00:00
}
2022-03-12 17:48:54 +00:00
func (that *CacheRedis) reCon() bool {
2018-04-04 18:44:00 +00:00
var err error
2022-03-12 17:48:54 +00:00
that.conn, err = redis.Dial("tcp", that.Host+":"+ObjToStr(that.Port))
if err != nil {
2022-03-12 17:48:54 +00:00
that.conn = nil
that.Error.SetError(err)
2018-04-04 18:44:00 +00:00
return false
}
2022-03-12 17:48:54 +00:00
if that.Pwd != "" {
_, err = that.conn.Do("AUTH", that.Pwd)
2021-05-28 15:48:33 +00:00
if err != nil {
2022-03-12 17:48:54 +00:00
that.conn = nil
that.Error.SetError(err)
2021-05-28 15:48:33 +00:00
return false
}
2018-04-04 18:44:00 +00:00
}
2021-05-28 15:48:33 +00:00
2018-04-04 18:44:00 +00:00
return true
}
2022-03-12 17:48:54 +00:00
func (that *CacheRedis) del(key string) {
del := strings.Index(key, "*")
if del != -1 {
2022-03-12 17:48:54 +00:00
val, err := redis.Strings(that.conn.Do("KEYS", key))
if err != nil {
2018-04-09 17:16:24 +00:00
return
}
2022-03-12 17:48:54 +00:00
that.conn.Send("MULTI")
2018-04-09 17:16:24 +00:00
for i, _ := range val {
2022-03-12 17:48:54 +00:00
that.conn.Send("DEL", val[i])
2018-04-09 17:16:24 +00:00
}
2022-03-12 17:48:54 +00:00
that.conn.Do("EXEC")
} else {
2022-03-12 17:48:54 +00:00
_, err := that.conn.Do("DEL", key)
if err != nil {
2022-03-12 17:48:54 +00:00
that.Error.SetError(err)
_, err = that.conn.Do("PING")
if err != nil {
2022-03-12 17:48:54 +00:00
if that.reCon() {
_, err = that.conn.Do("DEL", key)
2018-04-09 17:16:24 +00:00
}
}
}
}
}
2018-04-04 18:44:00 +00:00
2018-04-09 17:16:24 +00:00
//key value ,时间为时间戳
2022-03-12 17:48:54 +00:00
func (that *CacheRedis) set(key string, value string, time int64) {
_, err := that.conn.Do("SET", key, value, "EX", ObjToStr(time))
if err != nil {
2018-04-09 17:16:24 +00:00
2022-03-12 17:48:54 +00:00
that.Error.SetError(err)
_, err = that.conn.Do("PING")
if err != nil {
2022-03-12 17:48:54 +00:00
if that.reCon() {
_, err = that.conn.Do("SET", key, value, "EX", ObjToStr(time))
2018-04-09 17:16:24 +00:00
}
}
}
}
2022-03-12 17:48:54 +00:00
func (that *CacheRedis) get(key string) *Obj {
reData := &Obj{}
2018-04-04 18:44:00 +00:00
var err error
2022-03-12 17:48:54 +00:00
reData.Data, err = redis.String(that.conn.Do("GET", key))
2018-04-09 17:16:24 +00:00
if err != nil {
reData.Data = nil
2018-04-09 17:16:24 +00:00
if !strings.Contains(err.Error(), "nil returned") {
2022-03-12 17:48:54 +00:00
that.Error.SetError(err)
_, err = that.conn.Do("PING")
2018-04-09 17:16:24 +00:00
if err != nil {
2022-03-12 17:48:54 +00:00
if that.reCon() {
reData.Data, err = redis.String(that.conn.Do("GET", key))
2018-04-09 17:16:24 +00:00
}
}
return reData
}
}
return reData
}
2022-03-12 17:48:54 +00:00
func (that *CacheRedis) Cache(key string, data ...interface{}) *Obj {
reData := &Obj{}
2022-03-12 17:48:54 +00:00
if that.conn == nil {
re := that.reCon()
if !re {
2018-04-04 18:44:00 +00:00
return reData
}
}
//查询缓存
if len(data) == 0 {
2022-03-12 17:48:54 +00:00
reData = that.get(key)
2018-04-09 17:16:24 +00:00
return reData
2018-04-04 18:44:00 +00:00
}
tim := int64(0)
//删除缓存
if len(data) == 1 && data[0] == nil {
2022-03-12 17:48:54 +00:00
that.del(key)
2018-04-04 18:44:00 +00:00
return reData
}
//添加缓存
if len(data) == 1 {
2022-03-12 17:48:54 +00:00
if that.TimeOut == 0 {
//that.Time = Config.GetInt64("cacheShortTime")
2018-04-04 18:44:00 +00:00
}
2022-03-12 17:48:54 +00:00
tim += that.TimeOut
2018-04-04 18:44:00 +00:00
}
if len(data) == 2 {
2022-03-12 17:48:54 +00:00
that.Error.SetError(nil)
tempt := ObjToInt64(data[1], that.Error)
2018-06-13 11:10:13 +00:00
if tempt > tim {
2018-04-04 18:44:00 +00:00
2018-06-13 11:10:13 +00:00
tim = tempt
2022-03-12 17:48:54 +00:00
} else if that.GetError() == nil {
2018-04-04 18:44:00 +00:00
2018-06-13 11:10:13 +00:00
tim = tim + tempt
2018-04-04 18:44:00 +00:00
}
}
2022-03-12 17:48:54 +00:00
that.set(key, ObjToStr(data[0]), tim)
2018-04-09 17:16:24 +00:00
2018-04-04 18:44:00 +00:00
return reData
}