|
@@ -3,10 +3,11 @@ package zhios_tool_cache |
|
|
import ( |
|
|
import ( |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"errors" |
|
|
"errors" |
|
|
redigo "github.com/gomodule/redigo/redis" |
|
|
|
|
|
"log" |
|
|
"log" |
|
|
"strings" |
|
|
"strings" |
|
|
"time" |
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
redigoRedis "github.com/gomodule/redigo/redis" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// configuration |
|
|
// configuration |
|
@@ -32,24 +33,24 @@ type Config struct { |
|
|
KeyPlaceholder string // placeholder to be parsed using given arguments to obtain a final key; example is "?" |
|
|
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 |
|
|
var conf *Config |
|
|
|
|
|
|
|
|
func NewRedis(addr string) { |
|
|
func NewRedis(addr string) { |
|
|
if addr == "" { |
|
|
if addr == "" { |
|
|
panic("\nredis connect string cannot be empty\n") |
|
|
panic("\nredis connect string cannot be empty\n") |
|
|
} |
|
|
} |
|
|
pool = &redigo.Pool{ |
|
|
|
|
|
|
|
|
pool = &redigoRedis.Pool{ |
|
|
MaxIdle: redisMaxIdleConn, |
|
|
MaxIdle: redisMaxIdleConn, |
|
|
IdleTimeout: redisIdleTTL, |
|
|
IdleTimeout: redisIdleTTL, |
|
|
MaxActive: redisMaxActive, |
|
|
MaxActive: redisMaxActive, |
|
|
// MaxConnLifetime: redisDialTTL, |
|
|
// MaxConnLifetime: redisDialTTL, |
|
|
Wait: true, |
|
|
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 { |
|
|
if err != nil { |
|
|
log.Println("Redis Dial failed: ", err) |
|
|
log.Println("Redis Dial failed: ", err) |
|
@@ -57,7 +58,7 @@ func NewRedis(addr string) { |
|
|
} |
|
|
} |
|
|
return c, err |
|
|
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") |
|
|
_, err := c.Do("PING") |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println("Unable to ping to redis server:", err) |
|
|
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...) |
|
|
return conn.Do(cmd, args...) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func GetPool() *redigo.Pool { |
|
|
|
|
|
|
|
|
func GetPool() *redigoRedis.Pool { |
|
|
return pool |
|
|
return pool |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -163,26 +164,26 @@ func Get(key string) (interface{}, error) { |
|
|
return Do("GET", key) |
|
|
return Do("GET", key) |
|
|
} |
|
|
} |
|
|
func GetTTL(key string) (time.Duration, error) { |
|
|
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 |
|
|
return time.Duration(ttl) * time.Second, err |
|
|
} |
|
|
} |
|
|
func GetBytes(key string) ([]byte, error) { |
|
|
func GetBytes(key string) ([]byte, error) { |
|
|
return redigo.Bytes(Do("GET", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Bytes(Do("GET", key)) |
|
|
} |
|
|
} |
|
|
func GetString(key string) (string, error) { |
|
|
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) { |
|
|
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) { |
|
|
func GetInt(key string) (int, error) { |
|
|
return redigo.Int(Do("GET", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int(Do("GET", key)) |
|
|
} |
|
|
} |
|
|
func GetInt64(key string) (int64, error) { |
|
|
func GetInt64(key string) (int64, error) { |
|
|
return redigo.Int64(Do("GET", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int64(Do("GET", key)) |
|
|
} |
|
|
} |
|
|
func GetStringLength(key string) (int, error) { |
|
|
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) { |
|
|
func ZAdd(key string, score float64, data interface{}) (interface{}, error) { |
|
|
return Do("ZADD", key, score, data) |
|
|
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) { |
|
|
func ZRange(key string, start int, end int, withScores bool) ([]interface{}, error) { |
|
|
if withScores { |
|
|
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) { |
|
|
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) { |
|
|
func ZCard(setName string) (int64, error) { |
|
|
return redigo.Int64(Do("ZCARD", setName)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int64(Do("ZCARD", setName)) |
|
|
} |
|
|
} |
|
|
func ZScan(setName string) (int64, error) { |
|
|
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) { |
|
|
func SAdd(setName string, data interface{}) (interface{}, error) { |
|
|
return Do("SADD", setName, data) |
|
|
return Do("SADD", setName, data) |
|
|
} |
|
|
} |
|
|
func SCard(setName string) (int64, error) { |
|
|
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) { |
|
|
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) { |
|
|
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) { |
|
|
func SRem(setName string, data interface{}) (interface{}, error) { |
|
|
return Do("SREM", setName, data) |
|
|
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) { |
|
|
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) { |
|
|
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 |
|
|
return f, err |
|
|
} |
|
|
} |
|
|
func HGetInt(key string, HKey string) (int, error) { |
|
|
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) { |
|
|
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) { |
|
|
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) { |
|
|
func HDel(key string, HKey string) (interface{}, error) { |
|
|
return Do("HDEL", key, HKey) |
|
|
return Do("HDEL", key, HKey) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func HGetAll(key string) (map[string]interface{}, error) { |
|
|
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 { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
|
num := len(vals) / 2 |
|
|
num := len(vals) / 2 |
|
|
result := make(map[string]interface{}, num) |
|
|
result := make(map[string]interface{}, num) |
|
|
for i := 0; i < num; i++ { |
|
|
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] |
|
|
result[key] = vals[2*i+1] |
|
|
} |
|
|
} |
|
|
return result, nil |
|
|
return result, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func FlushAll() bool { |
|
|
func FlushAll() bool { |
|
|
res, _ := redigo.String(Do("FLUSHALL")) |
|
|
|
|
|
|
|
|
res, _ := redigoRedis.String(Do("FLUSHALL")) |
|
|
if res == "" { |
|
|
if res == "" { |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
@@ -298,17 +299,17 @@ func FlushAll() bool { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// NOTE: Use this in production environment with extreme care. |
|
|
// 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) { |
|
|
func Keys(pattern string) ([]string, error) { |
|
|
return redigo.Strings(Do("KEYS", pattern)) |
|
|
|
|
|
|
|
|
return redigoRedis.Strings(Do("KEYS", pattern)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func HKeys(key string) ([]string, error) { |
|
|
func HKeys(key string) ([]string, error) { |
|
|
return redigo.Strings(Do("HKEYS", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Strings(Do("HKEYS", key)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Exists(key string) bool { |
|
|
func Exists(key string) bool { |
|
|
count, err := redigo.Int(Do("EXISTS", key)) |
|
|
|
|
|
|
|
|
count, err := redigoRedis.Int(Do("EXISTS", key)) |
|
|
if count == 0 || err != nil { |
|
|
if count == 0 || err != nil { |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
@@ -316,27 +317,27 @@ func Exists(key string) bool { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Incr(key string) (int64, error) { |
|
|
func Incr(key string) (int64, error) { |
|
|
return redigo.Int64(Do("INCR", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int64(Do("INCR", key)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Decr(key string) (int64, error) { |
|
|
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) { |
|
|
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) { |
|
|
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) { |
|
|
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) { |
|
|
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 |
|
|
// use for message queue |
|
@@ -350,17 +351,17 @@ func LPop(key string) (interface{}, error) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func LPopString(key string) (string, 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) { |
|
|
func LPopFloat(key string) (float64, error) { |
|
|
f, err := redigo.Float64(Do("LPOP", key)) |
|
|
|
|
|
|
|
|
f, err := redigoRedis.Float64(Do("LPOP", key)) |
|
|
return f, err |
|
|
return f, err |
|
|
} |
|
|
} |
|
|
func LPopInt(key string) (int, error) { |
|
|
func LPopInt(key string) (int, error) { |
|
|
return redigo.Int(Do("LPOP", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int(Do("LPOP", key)) |
|
|
} |
|
|
} |
|
|
func LPopInt64(key string) (int64, error) { |
|
|
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) { |
|
|
func RPush(key string, data interface{}) (interface{}, error) { |
|
@@ -373,28 +374,28 @@ func RPop(key string) (interface{}, error) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func RPopString(key string) (string, 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) { |
|
|
func RPopFloat(key string) (float64, error) { |
|
|
f, err := redigo.Float64(Do("RPOP", key)) |
|
|
|
|
|
|
|
|
f, err := redigoRedis.Float64(Do("RPOP", key)) |
|
|
return f, err |
|
|
return f, err |
|
|
} |
|
|
} |
|
|
func RPopInt(key string) (int, error) { |
|
|
func RPopInt(key string) (int, error) { |
|
|
return redigo.Int(Do("RPOP", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int(Do("RPOP", key)) |
|
|
} |
|
|
} |
|
|
func RPopInt64(key string) (int64, error) { |
|
|
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) { |
|
|
func Scan(cursor int64, pattern string, count int64) (int64, []string, error) { |
|
|
var items []string |
|
|
var items []string |
|
|
var newCursor int64 |
|
|
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 { |
|
|
if err != nil { |
|
|
return 0, nil, err |
|
|
return 0, nil, err |
|
|
} |
|
|
} |
|
|
values, err = redigo.Scan(values, &newCursor, &items) |
|
|
|
|
|
|
|
|
values, err = redigoRedis.Scan(values, &newCursor, &items) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return 0, nil, err |
|
|
return 0, nil, err |
|
|
} |
|
|
} |
|
|