hotime/cache/cache_redis.go

191 lines
3.6 KiB
Go
Raw Permalink Normal View History

2021-05-24 07:27:41 +08:00
package cache
2018-04-04 18:44:00 +00:00
import (
2022-03-13 01:12:29 +08:00
. "code.hoteas.com/golang/hotime/common"
2018-04-04 18:44:00 +00:00
"github.com/garyburd/redigo/redis"
"strings"
"sync"
"time"
2018-04-04 18:44:00 +00:00
)
type CacheRedis struct {
TimeOut int64
DbSet bool
SessionSet bool
Host string
Pwd string
Port int64
pool *redis.Pool
tag int64
2021-05-25 20:27:24 +08:00
ContextBase
*Error
initOnce sync.Once
}
2022-03-13 01:48:54 +08:00
func (that *CacheRedis) GetError() *Error {
2022-03-13 01:48:54 +08:00
return that.Error
}
2022-03-13 01:48:54 +08:00
func (that *CacheRedis) SetError(err *Error) {
that.Error = err
2018-04-04 18:44:00 +00:00
}
// 唯一标志
2022-03-13 01:48:54 +08:00
func (that *CacheRedis) GetTag() int64 {
2018-04-04 18:44:00 +00:00
2022-03-13 01:48:54 +08:00
if that.tag == int64(0) {
that.tag = time.Now().UnixNano()
2018-04-04 18:44:00 +00:00
}
2022-03-13 01:48:54 +08:00
return that.tag
2018-04-04 18:44:00 +00:00
}
// initPool 初始化连接池(只执行一次)
func (that *CacheRedis) initPool() {
that.initOnce.Do(func() {
that.pool = &redis.Pool{
MaxIdle: 10, // 最大空闲连接数
MaxActive: 100, // 最大活跃连接数0表示无限制
IdleTimeout: 5 * time.Minute, // 空闲连接超时时间
Wait: true, // 当连接池耗尽时是否等待
Dial: func() (redis.Conn, error) {
conn, err := redis.Dial("tcp", that.Host+":"+ObjToStr(that.Port),
redis.DialConnectTimeout(5*time.Second),
redis.DialReadTimeout(3*time.Second),
redis.DialWriteTimeout(3*time.Second),
)
if err != nil {
return nil, err
}
if that.Pwd != "" {
if _, err := conn.Do("AUTH", that.Pwd); err != nil {
conn.Close()
return nil, err
}
}
return conn, nil
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
if time.Since(t) < time.Minute {
return nil
}
_, err := c.Do("PING")
return err
},
2021-05-28 23:48:33 +08:00
}
})
}
2021-05-28 23:48:33 +08:00
// getConn 从连接池获取连接
func (that *CacheRedis) getConn() redis.Conn {
that.initPool()
if that.pool == nil {
return nil
}
return that.pool.Get()
2018-04-04 18:44:00 +00:00
}
2022-03-13 01:48:54 +08:00
func (that *CacheRedis) del(key string) {
conn := that.getConn()
if conn == nil {
return
}
defer conn.Close()
del := strings.Index(key, "*")
if del != -1 {
val, err := redis.Strings(conn.Do("KEYS", key))
if err != nil {
that.Error.SetError(err)
2018-04-09 17:16:24 +00:00
return
}
if len(val) == 0 {
return
}
conn.Send("MULTI")
for i := range val {
conn.Send("DEL", val[i])
}
_, err = conn.Do("EXEC")
if err != nil {
that.Error.SetError(err)
2018-04-09 17:16:24 +00:00
}
} else {
_, err := conn.Do("DEL", key)
if err != nil {
2022-03-13 01:48:54 +08:00
that.Error.SetError(err)
2018-04-09 17:16:24 +00:00
}
}
}
2018-04-04 18:44:00 +00:00
// key value ,时间为时间戳
func (that *CacheRedis) set(key string, value string, expireSeconds int64) {
conn := that.getConn()
if conn == nil {
return
}
defer conn.Close()
2018-04-09 17:16:24 +00:00
_, err := conn.Do("SET", key, value, "EX", ObjToStr(expireSeconds))
if err != nil {
2022-03-13 01:48:54 +08:00
that.Error.SetError(err)
2018-04-09 17:16:24 +00:00
}
}
2022-03-13 01:48:54 +08:00
func (that *CacheRedis) get(key string) *Obj {
reData := &Obj{}
conn := that.getConn()
if conn == nil {
return reData
}
defer conn.Close()
2018-04-04 18:44:00 +00:00
var err error
reData.Data, err = redis.String(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-13 01:48:54 +08:00
that.Error.SetError(err)
2018-04-09 17:16:24 +00:00
}
}
return reData
}
2022-03-13 01:48:54 +08:00
func (that *CacheRedis) Cache(key string, data ...interface{}) *Obj {
reData := &Obj{}
2018-04-04 18:44:00 +00:00
//查询缓存
if len(data) == 0 {
2022-03-13 01:48:54 +08:00
reData = that.get(key)
2018-04-09 17:16:24 +00:00
return reData
2018-04-04 18:44:00 +00:00
}
2018-04-04 18:44:00 +00:00
tim := int64(0)
//删除缓存
if len(data) == 1 && data[0] == nil {
2022-03-13 01:48:54 +08:00
that.del(key)
2018-04-04 18:44:00 +00:00
return reData
}
//添加缓存
if len(data) == 1 {
2022-03-13 01:48:54 +08:00
if that.TimeOut == 0 {
//that.Time = Config.GetInt64("cacheShortTime")
2018-04-04 18:44:00 +00:00
}
2022-03-13 01:48:54 +08:00
tim += that.TimeOut
2018-04-04 18:44:00 +00:00
}
if len(data) == 2 {
2022-03-13 01:48:54 +08:00
that.Error.SetError(nil)
tempt := ObjToInt64(data[1], that.Error)
2018-06-13 11:10:13 +00:00
if tempt > tim {
tim = tempt
2022-03-13 01:48:54 +08:00
} else if that.GetError() == nil {
2018-06-13 11:10:13 +00:00
tim = tim + tempt
2018-04-04 18:44:00 +00:00
}
}
2022-03-13 01:48:54 +08: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
}