|
|
@@ -3,10 +3,11 @@ package zhios_tool_cache |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
redigo "github.com/gomodule/redigo/redis" |
|
|
|
"log" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
|
|
|
|
redigoRedis "github.com/gomodule/redigo/redis" |
|
|
|
) |
|
|
|
|
|
|
|
// configuration |
|
|
@@ -32,24 +33,24 @@ type Config struct { |
|
|
|
KeyPlaceholder string // placeholder to be parsed using given arguments to obtain a final key; example is "?" |
|
|
|
} |
|
|
|
|
|
|
|
var pool *redigo.Pool |
|
|
|
var pool *redigoRedis.Pool |
|
|
|
var conf *Config |
|
|
|
|
|
|
|
func NewRedis(addr string) { |
|
|
|
if addr == "" { |
|
|
|
panic("\nredis connect string cannot be empty\n") |
|
|
|
} |
|
|
|
pool = &redigo.Pool{ |
|
|
|
pool = &redigoRedis.Pool{ |
|
|
|
MaxIdle: redisMaxIdleConn, |
|
|
|
IdleTimeout: redisIdleTTL, |
|
|
|
MaxActive: redisMaxActive, |
|
|
|
// MaxConnLifetime: redisDialTTL, |
|
|
|
Wait: true, |
|
|
|
Dial: func() (redigo.Conn, error) { |
|
|
|
c, err := redigo.Dial("tcp", addr, |
|
|
|
redigo.DialConnectTimeout(redisDialTTL), |
|
|
|
redigo.DialReadTimeout(redisReadTTL), |
|
|
|
redigo.DialWriteTimeout(redisWriteTTL), |
|
|
|
Dial: func() (redigoRedis.Conn, error) { |
|
|
|
c, err := redigoRedis.Dial("tcp", addr, |
|
|
|
redigoRedis.DialConnectTimeout(redisDialTTL), |
|
|
|
redigoRedis.DialReadTimeout(redisReadTTL), |
|
|
|
redigoRedis.DialWriteTimeout(redisWriteTTL), |
|
|
|
) |
|
|
|
if err != nil { |
|
|
|
log.Println("Redis Dial failed: ", err) |
|
|
@@ -57,7 +58,7 @@ func NewRedis(addr string) { |
|
|
|
} |
|
|
|
return c, err |
|
|
|
}, |
|
|
|
TestOnBorrow: func(c redigo.Conn, t time.Time) error { |
|
|
|
TestOnBorrow: func(c redigoRedis.Conn, t time.Time) error { |
|
|
|
_, err := c.Do("PING") |
|
|
|
if err != nil { |
|
|
|
log.Println("Unable to ping to redis server:", err) |
|
|
@@ -80,7 +81,7 @@ func Do(cmd string, args ...interface{}) (reply interface{}, err error) { |
|
|
|
return conn.Do(cmd, args...) |
|
|
|
} |
|
|
|
|
|
|
|
func GetPool() *redigo.Pool { |
|
|
|
func GetPool() *redigoRedis.Pool { |
|
|
|
return pool |
|
|
|
} |
|
|
|
|
|
|
@@ -163,26 +164,26 @@ func Get(key string) (interface{}, error) { |
|
|
|
return Do("GET", key) |
|
|
|
} |
|
|
|
func GetTTL(key string) (time.Duration, error) { |
|
|
|
ttl, err := redigo.Int64(Do("TTL", key)) |
|
|
|
ttl, err := redigoRedis.Int64(Do("TTL", key)) |
|
|
|
return time.Duration(ttl) * time.Second, err |
|
|
|
} |
|
|
|
func GetBytes(key string) ([]byte, error) { |
|
|
|
return redigo.Bytes(Do("GET", key)) |
|
|
|
return redigoRedis.Bytes(Do("GET", key)) |
|
|
|
} |
|
|
|
func GetString(key string) (string, error) { |
|
|
|
return redigo.String(Do("GET", key)) |
|
|
|
return redigoRedis.String(Do("GET", key)) |
|
|
|
} |
|
|
|
func GetStringMap(key string) (map[string]string, error) { |
|
|
|
return redigo.StringMap(Do("GET", key)) |
|
|
|
return redigoRedis.StringMap(Do("GET", key)) |
|
|
|
} |
|
|
|
func GetInt(key string) (int, error) { |
|
|
|
return redigo.Int(Do("GET", key)) |
|
|
|
return redigoRedis.Int(Do("GET", key)) |
|
|
|
} |
|
|
|
func GetInt64(key string) (int64, error) { |
|
|
|
return redigo.Int64(Do("GET", key)) |
|
|
|
return redigoRedis.Int64(Do("GET", key)) |
|
|
|
} |
|
|
|
func GetStringLength(key string) (int, error) { |
|
|
|
return redigo.Int(Do("STRLEN", key)) |
|
|
|
return redigoRedis.Int(Do("STRLEN", key)) |
|
|
|
} |
|
|
|
func ZAdd(key string, score float64, data interface{}) (interface{}, error) { |
|
|
|
return Do("ZADD", key, score, data) |
|
|
@@ -195,30 +196,30 @@ func ZRem(key string, data interface{}) (interface{}, error) { |
|
|
|
} |
|
|
|
func ZRange(key string, start int, end int, withScores bool) ([]interface{}, error) { |
|
|
|
if withScores { |
|
|
|
return redigo.Values(Do("ZRANGE", key, start, end, "WITHSCORES")) |
|
|
|
return redigoRedis.Values(Do("ZRANGE", key, start, end, "WITHSCORES")) |
|
|
|
} |
|
|
|
return redigo.Values(Do("ZRANGE", key, start, end)) |
|
|
|
return redigoRedis.Values(Do("ZRANGE", key, start, end)) |
|
|
|
} |
|
|
|
func ZRemRangeByScore(key string, start int64, end int64) ([]interface{}, error) { |
|
|
|
return redigo.Values(Do("ZREMRANGEBYSCORE", key, start, end)) |
|
|
|
return redigoRedis.Values(Do("ZREMRANGEBYSCORE", key, start, end)) |
|
|
|
} |
|
|
|
func ZCard(setName string) (int64, error) { |
|
|
|
return redigo.Int64(Do("ZCARD", setName)) |
|
|
|
return redigoRedis.Int64(Do("ZCARD", setName)) |
|
|
|
} |
|
|
|
func ZScan(setName string) (int64, error) { |
|
|
|
return redigo.Int64(Do("ZCARD", setName)) |
|
|
|
return redigoRedis.Int64(Do("ZCARD", setName)) |
|
|
|
} |
|
|
|
func SAdd(setName string, data interface{}) (interface{}, error) { |
|
|
|
return Do("SADD", setName, data) |
|
|
|
} |
|
|
|
func SCard(setName string) (int64, error) { |
|
|
|
return redigo.Int64(Do("SCARD", setName)) |
|
|
|
return redigoRedis.Int64(Do("SCARD", setName)) |
|
|
|
} |
|
|
|
func SIsMember(setName string, data interface{}) (bool, error) { |
|
|
|
return redigo.Bool(Do("SISMEMBER", setName, data)) |
|
|
|
return redigoRedis.Bool(Do("SISMEMBER", setName, data)) |
|
|
|
} |
|
|
|
func SMembers(setName string) ([]string, error) { |
|
|
|
return redigo.Strings(Do("SMEMBERS", setName)) |
|
|
|
return redigoRedis.Strings(Do("SMEMBERS", setName)) |
|
|
|
} |
|
|
|
func SRem(setName string, data interface{}) (interface{}, error) { |
|
|
|
return Do("SREM", setName, data) |
|
|
@@ -256,41 +257,41 @@ func HMSet(key string, hashKeys []string, vals []interface{}) (interface{}, erro |
|
|
|
} |
|
|
|
|
|
|
|
func HGetString(key string, HKey string) (string, error) { |
|
|
|
return redigo.String(Do("HGET", key, HKey)) |
|
|
|
return redigoRedis.String(Do("HGET", key, HKey)) |
|
|
|
} |
|
|
|
func HGetFloat(key string, HKey string) (float64, error) { |
|
|
|
f, err := redigo.Float64(Do("HGET", key, HKey)) |
|
|
|
f, err := redigoRedis.Float64(Do("HGET", key, HKey)) |
|
|
|
return f, err |
|
|
|
} |
|
|
|
func HGetInt(key string, HKey string) (int, error) { |
|
|
|
return redigo.Int(Do("HGET", key, HKey)) |
|
|
|
return redigoRedis.Int(Do("HGET", key, HKey)) |
|
|
|
} |
|
|
|
func HGetInt64(key string, HKey string) (int64, error) { |
|
|
|
return redigo.Int64(Do("HGET", key, HKey)) |
|
|
|
return redigoRedis.Int64(Do("HGET", key, HKey)) |
|
|
|
} |
|
|
|
func HGetBool(key string, HKey string) (bool, error) { |
|
|
|
return redigo.Bool(Do("HGET", key, HKey)) |
|
|
|
return redigoRedis.Bool(Do("HGET", key, HKey)) |
|
|
|
} |
|
|
|
func HDel(key string, HKey string) (interface{}, error) { |
|
|
|
return Do("HDEL", key, HKey) |
|
|
|
} |
|
|
|
|
|
|
|
func HGetAll(key string) (map[string]interface{}, error) { |
|
|
|
vals, err := redigo.Values(Do("HGETALL", key)) |
|
|
|
vals, err := redigoRedis.Values(Do("HGETALL", key)) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
num := len(vals) / 2 |
|
|
|
result := make(map[string]interface{}, num) |
|
|
|
for i := 0; i < num; i++ { |
|
|
|
key, _ := redigo.String(vals[2*i], nil) |
|
|
|
key, _ := redigoRedis.String(vals[2*i], nil) |
|
|
|
result[key] = vals[2*i+1] |
|
|
|
} |
|
|
|
return result, nil |
|
|
|
} |
|
|
|
|
|
|
|
func FlushAll() bool { |
|
|
|
res, _ := redigo.String(Do("FLUSHALL")) |
|
|
|
res, _ := redigoRedis.String(Do("FLUSHALL")) |
|
|
|
if res == "" { |
|
|
|
return false |
|
|
|
} |
|
|
@@ -298,17 +299,17 @@ func FlushAll() bool { |
|
|
|
} |
|
|
|
|
|
|
|
// NOTE: Use this in production environment with extreme care. |
|
|
|
// Read more here:https://redigo.io/commands/keys |
|
|
|
// Read more here:https://redigoRedis.io/commands/keys |
|
|
|
func Keys(pattern string) ([]string, error) { |
|
|
|
return redigo.Strings(Do("KEYS", pattern)) |
|
|
|
return redigoRedis.Strings(Do("KEYS", pattern)) |
|
|
|
} |
|
|
|
|
|
|
|
func HKeys(key string) ([]string, error) { |
|
|
|
return redigo.Strings(Do("HKEYS", key)) |
|
|
|
return redigoRedis.Strings(Do("HKEYS", key)) |
|
|
|
} |
|
|
|
|
|
|
|
func Exists(key string) bool { |
|
|
|
count, err := redigo.Int(Do("EXISTS", key)) |
|
|
|
count, err := redigoRedis.Int(Do("EXISTS", key)) |
|
|
|
if count == 0 || err != nil { |
|
|
|
return false |
|
|
|
} |
|
|
@@ -316,27 +317,27 @@ func Exists(key string) bool { |
|
|
|
} |
|
|
|
|
|
|
|
func Incr(key string) (int64, error) { |
|
|
|
return redigo.Int64(Do("INCR", key)) |
|
|
|
return redigoRedis.Int64(Do("INCR", key)) |
|
|
|
} |
|
|
|
|
|
|
|
func Decr(key string) (int64, error) { |
|
|
|
return redigo.Int64(Do("DECR", key)) |
|
|
|
return redigoRedis.Int64(Do("DECR", key)) |
|
|
|
} |
|
|
|
|
|
|
|
func IncrBy(key string, incBy int64) (int64, error) { |
|
|
|
return redigo.Int64(Do("INCRBY", key, incBy)) |
|
|
|
return redigoRedis.Int64(Do("INCRBY", key, incBy)) |
|
|
|
} |
|
|
|
|
|
|
|
func DecrBy(key string, decrBy int64) (int64, error) { |
|
|
|
return redigo.Int64(Do("DECRBY", key)) |
|
|
|
return redigoRedis.Int64(Do("DECRBY", key)) |
|
|
|
} |
|
|
|
|
|
|
|
func IncrByFloat(key string, incBy float64) (float64, error) { |
|
|
|
return redigo.Float64(Do("INCRBYFLOAT", key, incBy)) |
|
|
|
return redigoRedis.Float64(Do("INCRBYFLOAT", key, incBy)) |
|
|
|
} |
|
|
|
|
|
|
|
func DecrByFloat(key string, decrBy float64) (float64, error) { |
|
|
|
return redigo.Float64(Do("DECRBYFLOAT", key, decrBy)) |
|
|
|
return redigoRedis.Float64(Do("DECRBYFLOAT", key, decrBy)) |
|
|
|
} |
|
|
|
|
|
|
|
// use for message queue |
|
|
@@ -350,17 +351,17 @@ func LPop(key string) (interface{}, error) { |
|
|
|
} |
|
|
|
|
|
|
|
func LPopString(key string) (string, error) { |
|
|
|
return redigo.String(Do("LPOP", key)) |
|
|
|
return redigoRedis.String(Do("LPOP", key)) |
|
|
|
} |
|
|
|
func LPopFloat(key string) (float64, error) { |
|
|
|
f, err := redigo.Float64(Do("LPOP", key)) |
|
|
|
f, err := redigoRedis.Float64(Do("LPOP", key)) |
|
|
|
return f, err |
|
|
|
} |
|
|
|
func LPopInt(key string) (int, error) { |
|
|
|
return redigo.Int(Do("LPOP", key)) |
|
|
|
return redigoRedis.Int(Do("LPOP", key)) |
|
|
|
} |
|
|
|
func LPopInt64(key string) (int64, error) { |
|
|
|
return redigo.Int64(Do("LPOP", key)) |
|
|
|
return redigoRedis.Int64(Do("LPOP", key)) |
|
|
|
} |
|
|
|
|
|
|
|
func RPush(key string, data interface{}) (interface{}, error) { |
|
|
@@ -373,28 +374,28 @@ func RPop(key string) (interface{}, error) { |
|
|
|
} |
|
|
|
|
|
|
|
func RPopString(key string) (string, error) { |
|
|
|
return redigo.String(Do("RPOP", key)) |
|
|
|
return redigoRedis.String(Do("RPOP", key)) |
|
|
|
} |
|
|
|
func RPopFloat(key string) (float64, error) { |
|
|
|
f, err := redigo.Float64(Do("RPOP", key)) |
|
|
|
f, err := redigoRedis.Float64(Do("RPOP", key)) |
|
|
|
return f, err |
|
|
|
} |
|
|
|
func RPopInt(key string) (int, error) { |
|
|
|
return redigo.Int(Do("RPOP", key)) |
|
|
|
return redigoRedis.Int(Do("RPOP", key)) |
|
|
|
} |
|
|
|
func RPopInt64(key string) (int64, error) { |
|
|
|
return redigo.Int64(Do("RPOP", key)) |
|
|
|
return redigoRedis.Int64(Do("RPOP", key)) |
|
|
|
} |
|
|
|
|
|
|
|
func Scan(cursor int64, pattern string, count int64) (int64, []string, error) { |
|
|
|
var items []string |
|
|
|
var newCursor int64 |
|
|
|
|
|
|
|
values, err := redigo.Values(Do("SCAN", cursor, "MATCH", pattern, "COUNT", count)) |
|
|
|
values, err := redigoRedis.Values(Do("SCAN", cursor, "MATCH", pattern, "COUNT", count)) |
|
|
|
if err != nil { |
|
|
|
return 0, nil, err |
|
|
|
} |
|
|
|
values, err = redigo.Scan(values, &newCursor, &items) |
|
|
|
values, err = redigoRedis.Scan(values, &newCursor, &items) |
|
|
|
if err != nil { |
|
|
|
return 0, nil, err |
|
|
|
} |
|
|
|