|
@@ -6,21 +6,21 @@ import ( |
|
|
"strings" |
|
|
"strings" |
|
|
"time" |
|
|
"time" |
|
|
|
|
|
|
|
|
redigo "github.com/gomodule/redigo/redis" |
|
|
|
|
|
|
|
|
redigoRedis "github.com/gomodule/redigo/redis" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type RedisPool struct { |
|
|
type RedisPool struct { |
|
|
*redigo.Pool |
|
|
|
|
|
|
|
|
*redigoRedis.Pool |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func NewRedisPool(cfg *Config) *RedisPool { |
|
|
func NewRedisPool(cfg *Config) *RedisPool { |
|
|
return &RedisPool{&redigo.Pool{ |
|
|
|
|
|
|
|
|
return &RedisPool{&redigoRedis.Pool{ |
|
|
MaxIdle: cfg.MaxIdle, |
|
|
MaxIdle: cfg.MaxIdle, |
|
|
IdleTimeout: cfg.IdleTimeout, |
|
|
IdleTimeout: cfg.IdleTimeout, |
|
|
MaxActive: cfg.MaxActive, |
|
|
MaxActive: cfg.MaxActive, |
|
|
Wait: cfg.Wait, |
|
|
Wait: cfg.Wait, |
|
|
Dial: func() (redigo.Conn, error) { |
|
|
|
|
|
c, err := redigo.Dial("tcp", cfg.Server) |
|
|
|
|
|
|
|
|
Dial: func() (redigoRedis.Conn, error) { |
|
|
|
|
|
c, err := redigoRedis.Dial("tcp", cfg.Server) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println("Redis Dial failed: ", err) |
|
|
log.Println("Redis Dial failed: ", err) |
|
|
return nil, err |
|
|
return nil, err |
|
@@ -34,7 +34,7 @@ func NewRedisPool(cfg *Config) *RedisPool { |
|
|
} |
|
|
} |
|
|
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) |
|
@@ -50,7 +50,7 @@ func (p *RedisPool) Do(cmd string, args ...interface{}) (reply interface{}, err |
|
|
return conn.Do(cmd, args...) |
|
|
return conn.Do(cmd, args...) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) GetPool() *redigo.Pool { |
|
|
|
|
|
|
|
|
func (p *RedisPool) GetPool() *redigoRedis.Pool { |
|
|
return pool |
|
|
return pool |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -106,24 +106,24 @@ func (p *RedisPool) Get(key string) (interface{}, error) { |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) GetStringMap(key string) (map[string]string, error) { |
|
|
func (p *RedisPool) GetStringMap(key string) (map[string]string, error) { |
|
|
// get |
|
|
// get |
|
|
return redigo.StringMap(Do("GET", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.StringMap(Do("GET", key)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) GetTTL(key string) (time.Duration, error) { |
|
|
func (p *RedisPool) 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 (p *RedisPool) GetBytes(key string) ([]byte, error) { |
|
|
func (p *RedisPool) GetBytes(key string) ([]byte, error) { |
|
|
return redigo.Bytes(Do("GET", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Bytes(Do("GET", key)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) GetString(key string) (string, error) { |
|
|
func (p *RedisPool) GetString(key string) (string, error) { |
|
|
return redigo.String(Do("GET", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.String(Do("GET", key)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) GetInt(key string) (int, error) { |
|
|
func (p *RedisPool) GetInt(key string) (int, error) { |
|
|
return redigo.Int(Do("GET", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int(Do("GET", key)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) GetStringLength(key string) (int, error) { |
|
|
func (p *RedisPool) GetStringLength(key string) (int, error) { |
|
|
return redigo.Int(Do("STRLEN", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int(Do("STRLEN", key)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) ZAdd(key string, score float64, data interface{}) (interface{}, error) { |
|
|
func (p *RedisPool) ZAdd(key string, score float64, data interface{}) (interface{}, error) { |
|
|
return Do("ZADD", key, score, data) |
|
|
return Do("ZADD", key, score, data) |
|
@@ -133,21 +133,21 @@ func (p *RedisPool) ZRem(key string, data interface{}) (interface{}, error) { |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) ZRange(key string, start int, end int, withScores bool) ([]interface{}, error) { |
|
|
func (p *RedisPool) 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 (p *RedisPool) SAdd(setName string, data interface{}) (interface{}, error) { |
|
|
func (p *RedisPool) SAdd(setName string, data interface{}) (interface{}, error) { |
|
|
return Do("SADD", setName, data) |
|
|
return Do("SADD", setName, data) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) SCard(setName string) (int64, error) { |
|
|
func (p *RedisPool) SCard(setName string) (int64, error) { |
|
|
return redigo.Int64(Do("SCARD", setName)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int64(Do("SCARD", setName)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) SIsMember(setName string, data interface{}) (bool, error) { |
|
|
func (p *RedisPool) SIsMember(setName string, data interface{}) (bool, error) { |
|
|
return redigo.Bool(Do("SISMEMBER", setName, data)) |
|
|
|
|
|
|
|
|
return redigoRedis.Bool(Do("SISMEMBER", setName, data)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) SMembers(setName string) ([]string, error) { |
|
|
func (p *RedisPool) SMembers(setName string) ([]string, error) { |
|
|
return redigo.Strings(Do("SMEMBERS", setName)) |
|
|
|
|
|
|
|
|
return redigoRedis.Strings(Do("SMEMBERS", setName)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) SRem(setName string, data interface{}) (interface{}, error) { |
|
|
func (p *RedisPool) SRem(setName string, data interface{}) (interface{}, error) { |
|
|
return Do("SREM", setName, data) |
|
|
return Do("SREM", setName, data) |
|
@@ -185,50 +185,50 @@ func (p *RedisPool) HMSet(key string, hashKeys []string, vals []interface{}) (in |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) HGetString(key string, HKey string) (string, error) { |
|
|
func (p *RedisPool) HGetString(key string, HKey string) (string, error) { |
|
|
return redigo.String(Do("HGET", key, HKey)) |
|
|
|
|
|
|
|
|
return redigoRedis.String(Do("HGET", key, HKey)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) HGetFloat(key string, HKey string) (float64, error) { |
|
|
func (p *RedisPool) HGetFloat(key string, HKey string) (float64, error) { |
|
|
f, err := redigo.Float64(Do("HGET", key, HKey)) |
|
|
|
|
|
|
|
|
f, err := redigoRedis.Float64(Do("HGET", key, HKey)) |
|
|
return float64(f), err |
|
|
return float64(f), err |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) HGetInt(key string, HKey string) (int, error) { |
|
|
func (p *RedisPool) HGetInt(key string, HKey string) (int, error) { |
|
|
return redigo.Int(Do("HGET", key, HKey)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int(Do("HGET", key, HKey)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) HGetInt64(key string, HKey string) (int64, error) { |
|
|
func (p *RedisPool) HGetInt64(key string, HKey string) (int64, error) { |
|
|
return redigo.Int64(Do("HGET", key, HKey)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int64(Do("HGET", key, HKey)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) HGetBool(key string, HKey string) (bool, error) { |
|
|
func (p *RedisPool) HGetBool(key string, HKey string) (bool, error) { |
|
|
return redigo.Bool(Do("HGET", key, HKey)) |
|
|
|
|
|
|
|
|
return redigoRedis.Bool(Do("HGET", key, HKey)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) HDel(key string, HKey string) (interface{}, error) { |
|
|
func (p *RedisPool) HDel(key string, HKey string) (interface{}, error) { |
|
|
return Do("HDEL", key, HKey) |
|
|
return Do("HDEL", key, HKey) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) HGetAll(key string) (map[string]interface{}, error) { |
|
|
func (p *RedisPool) 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 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 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 (p *RedisPool) Keys(pattern string) ([]string, error) { |
|
|
func (p *RedisPool) Keys(pattern string) ([]string, error) { |
|
|
return redigo.Strings(Do("KEYS", pattern)) |
|
|
|
|
|
|
|
|
return redigoRedis.Strings(Do("KEYS", pattern)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) HKeys(key string) ([]string, error) { |
|
|
func (p *RedisPool) HKeys(key string) ([]string, error) { |
|
|
return redigo.Strings(Do("HKEYS", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Strings(Do("HKEYS", key)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) Exists(key string) (bool, error) { |
|
|
func (p *RedisPool) Exists(key string) (bool, error) { |
|
|
count, err := redigo.Int(Do("EXISTS", key)) |
|
|
|
|
|
|
|
|
count, err := redigoRedis.Int(Do("EXISTS", key)) |
|
|
if count == 0 { |
|
|
if count == 0 { |
|
|
return false, err |
|
|
return false, err |
|
|
} else { |
|
|
} else { |
|
@@ -237,27 +237,27 @@ func (p *RedisPool) Exists(key string) (bool, error) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) Incr(key string) (int64, error) { |
|
|
func (p *RedisPool) Incr(key string) (int64, error) { |
|
|
return redigo.Int64(Do("INCR", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int64(Do("INCR", key)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) Decr(key string) (int64, error) { |
|
|
func (p *RedisPool) Decr(key string) (int64, error) { |
|
|
return redigo.Int64(Do("DECR", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int64(Do("DECR", key)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) IncrBy(key string, incBy int64) (int64, error) { |
|
|
func (p *RedisPool) IncrBy(key string, incBy int64) (int64, error) { |
|
|
return redigo.Int64(Do("INCRBY", key, incBy)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int64(Do("INCRBY", key, incBy)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) DecrBy(key string, decrBy int64) (int64, error) { |
|
|
func (p *RedisPool) DecrBy(key string, decrBy int64) (int64, error) { |
|
|
return redigo.Int64(Do("DECRBY", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int64(Do("DECRBY", key)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) IncrByFloat(key string, incBy float64) (float64, error) { |
|
|
func (p *RedisPool) IncrByFloat(key string, incBy float64) (float64, error) { |
|
|
return redigo.Float64(Do("INCRBYFLOAT", key, incBy)) |
|
|
|
|
|
|
|
|
return redigoRedis.Float64(Do("INCRBYFLOAT", key, incBy)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) DecrByFloat(key string, decrBy float64) (float64, error) { |
|
|
func (p *RedisPool) 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 |
|
@@ -271,17 +271,17 @@ func (p *RedisPool) LPop(key string) (interface{}, error) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) LPopString(key string) (string, error) { |
|
|
func (p *RedisPool) LPopString(key string) (string, error) { |
|
|
return redigo.String(Do("LPOP", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.String(Do("LPOP", key)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) LPopFloat(key string) (float64, error) { |
|
|
func (p *RedisPool) LPopFloat(key string) (float64, error) { |
|
|
f, err := redigo.Float64(Do("LPOP", key)) |
|
|
|
|
|
|
|
|
f, err := redigoRedis.Float64(Do("LPOP", key)) |
|
|
return float64(f), err |
|
|
return float64(f), err |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) LPopInt(key string) (int, error) { |
|
|
func (p *RedisPool) LPopInt(key string) (int, error) { |
|
|
return redigo.Int(Do("LPOP", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int(Do("LPOP", key)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) LPopInt64(key string) (int64, error) { |
|
|
func (p *RedisPool) LPopInt64(key string) (int64, error) { |
|
|
return redigo.Int64(Do("LPOP", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int64(Do("LPOP", key)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) RPush(key string, data interface{}) (interface{}, error) { |
|
|
func (p *RedisPool) RPush(key string, data interface{}) (interface{}, error) { |
|
@@ -294,28 +294,28 @@ func (p *RedisPool) RPop(key string) (interface{}, error) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) RPopString(key string) (string, error) { |
|
|
func (p *RedisPool) RPopString(key string) (string, error) { |
|
|
return redigo.String(Do("RPOP", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.String(Do("RPOP", key)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) RPopFloat(key string) (float64, error) { |
|
|
func (p *RedisPool) RPopFloat(key string) (float64, error) { |
|
|
f, err := redigo.Float64(Do("RPOP", key)) |
|
|
|
|
|
|
|
|
f, err := redigoRedis.Float64(Do("RPOP", key)) |
|
|
return float64(f), err |
|
|
return float64(f), err |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) RPopInt(key string) (int, error) { |
|
|
func (p *RedisPool) RPopInt(key string) (int, error) { |
|
|
return redigo.Int(Do("RPOP", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int(Do("RPOP", key)) |
|
|
} |
|
|
} |
|
|
func (p *RedisPool) RPopInt64(key string) (int64, error) { |
|
|
func (p *RedisPool) RPopInt64(key string) (int64, error) { |
|
|
return redigo.Int64(Do("RPOP", key)) |
|
|
|
|
|
|
|
|
return redigoRedis.Int64(Do("RPOP", key)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *RedisPool) Scan(cursor int64, pattern string, count int64) (int64, []string, error) { |
|
|
func (p *RedisPool) 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 |
|
|
} |
|
|
} |
|
|