2021-05-23 23:27:41 +00:00
|
|
|
package cache
|
2018-04-04 18:44:00 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-23 23:27:41 +00:00
|
|
|
. "../common"
|
2018-04-04 18:44:00 +00:00
|
|
|
"github.com/garyburd/redigo/redis"
|
|
|
|
"strings"
|
2020-02-20 06:20:56 +00:00
|
|
|
"time"
|
2018-04-04 18:44:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CacheRedis struct {
|
|
|
|
Host string
|
2020-02-20 06:20:56 +00:00
|
|
|
Pwd string
|
2018-04-04 18:44:00 +00:00
|
|
|
Time int64
|
|
|
|
conn redis.Conn
|
2020-02-20 06:20:56 +00:00
|
|
|
tag int64
|
2018-04-04 18:44:00 +00:00
|
|
|
Error
|
|
|
|
}
|
|
|
|
|
|
|
|
//唯一标志
|
|
|
|
func (this *CacheRedis) GetTag() int64 {
|
|
|
|
|
|
|
|
if this.tag == int64(0) {
|
|
|
|
this.tag = time.Now().UnixNano()
|
|
|
|
}
|
|
|
|
return this.tag
|
|
|
|
}
|
|
|
|
|
2020-02-20 06:20:56 +00:00
|
|
|
func (this *CacheRedis) reCon() bool {
|
2018-04-04 18:44:00 +00:00
|
|
|
var err error
|
2020-02-20 06:20:56 +00:00
|
|
|
this.conn, err = redis.Dial("tcp", this.Host)
|
|
|
|
if err != nil {
|
|
|
|
this.conn = nil
|
2018-04-04 18:44:00 +00:00
|
|
|
this.Error.SetError(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-20 06:20:56 +00:00
|
|
|
_, err = this.conn.Do("AUTH", this.Pwd)
|
|
|
|
if err != nil {
|
|
|
|
this.conn = nil
|
2018-04-04 18:44:00 +00:00
|
|
|
this.Error.SetError(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2020-02-20 06:20:56 +00:00
|
|
|
func (this *CacheRedis) del(key string) {
|
|
|
|
del := strings.Index(key, "*")
|
|
|
|
if del != -1 {
|
2018-04-09 17:16:24 +00:00
|
|
|
val, err := redis.Strings(this.conn.Do("KEYS", key))
|
2020-02-20 06:20:56 +00:00
|
|
|
if err != nil {
|
2018-04-09 17:16:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
this.conn.Send("MULTI")
|
|
|
|
for i, _ := range val {
|
|
|
|
this.conn.Send("DEL", val[i])
|
|
|
|
}
|
|
|
|
this.conn.Do("EXEC")
|
2020-02-20 06:20:56 +00:00
|
|
|
} else {
|
|
|
|
_, err := this.conn.Do("DEL", key)
|
|
|
|
if err != nil {
|
2018-04-09 17:16:24 +00:00
|
|
|
this.Error.SetError(err)
|
2020-02-20 06:20:56 +00:00
|
|
|
_, err = this.conn.Do("PING")
|
|
|
|
if err != nil {
|
|
|
|
if this.reCon() {
|
|
|
|
_, err = this.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 ,时间为时间戳
|
|
|
|
func (this *CacheRedis) set(key string, value string, time int64) {
|
2020-02-20 06:20:56 +00:00
|
|
|
_, err := this.conn.Do("SET", key, value, "EX", ObjToStr(time))
|
|
|
|
if err != nil {
|
2018-04-09 17:16:24 +00:00
|
|
|
|
|
|
|
this.Error.SetError(err)
|
2020-02-20 06:20:56 +00:00
|
|
|
_, err = this.conn.Do("PING")
|
|
|
|
if err != nil {
|
|
|
|
if this.reCon() {
|
|
|
|
_, err = this.conn.Do("SET", key, value, "EX", ObjToStr(time))
|
2018-04-09 17:16:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 06:20:56 +00:00
|
|
|
func (this *CacheRedis) get(key string) *Obj {
|
|
|
|
reData := &Obj{}
|
2018-04-04 18:44:00 +00:00
|
|
|
var err error
|
2018-04-09 17:16:24 +00:00
|
|
|
reData.Data, err = redis.String(this.conn.Do("GET", key))
|
|
|
|
if err != nil {
|
2020-02-20 06:20:56 +00:00
|
|
|
reData.Data = nil
|
2018-04-09 17:16:24 +00:00
|
|
|
if !strings.Contains(err.Error(), "nil returned") {
|
|
|
|
this.Error.SetError(err)
|
|
|
|
_, err = this.conn.Do("PING")
|
|
|
|
if err != nil {
|
|
|
|
if this.reCon() {
|
|
|
|
reData.Data, err = redis.String(this.conn.Do("GET", key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return reData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reData
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *CacheRedis) Cache(key string, data ...interface{}) *Obj {
|
2020-02-20 06:20:56 +00:00
|
|
|
reData := &Obj{}
|
|
|
|
if this.conn == nil {
|
|
|
|
re := this.reCon()
|
|
|
|
if !re {
|
2018-04-04 18:44:00 +00:00
|
|
|
return reData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//查询缓存
|
|
|
|
if len(data) == 0 {
|
|
|
|
|
2020-02-20 06:20:56 +00:00
|
|
|
reData = this.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 {
|
2018-04-09 17:16:24 +00:00
|
|
|
this.del(key)
|
2018-04-04 18:44:00 +00:00
|
|
|
return reData
|
|
|
|
}
|
|
|
|
//添加缓存
|
|
|
|
if len(data) == 1 {
|
|
|
|
|
|
|
|
if this.Time == 0 {
|
2021-05-23 23:27:41 +00:00
|
|
|
//this.Time = Config.GetInt64("cacheShortTime")
|
2018-04-04 18:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tim += this.Time
|
|
|
|
}
|
|
|
|
if len(data) == 2 {
|
|
|
|
this.Error.SetError(nil)
|
|
|
|
tempt := ObjToInt64(data[1], &this.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
|
|
|
|
} else if this.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 06:20:56 +00:00
|
|
|
this.set(key, ObjToStr(data[0]), tim)
|
2018-04-09 17:16:24 +00:00
|
|
|
|
2018-04-04 18:44:00 +00:00
|
|
|
return reData
|
|
|
|
|
|
|
|
}
|