From 8dd1731b3f42ff1108526955eea6d00688d4bbde Mon Sep 17 00:00:00 2001 From: guzeng Date: Thu, 24 Mar 2022 10:03:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=89=E5=BA=8F=E9=9B=86?= =?UTF-8?q?=E5=90=88=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exists.go | 8 +- hash.go | 4 +- init.go | 12 ++- set.go | 82 -------------------- zset.go | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++ zset_test.go | 41 ++++++++++ 6 files changed, 269 insertions(+), 89 deletions(-) create mode 100644 zset.go create mode 100644 zset_test.go diff --git a/exists.go b/exists.go index 1ab1681..4985fe8 100644 --- a/exists.go +++ b/exists.go @@ -5,8 +5,8 @@ import ( ) //exists -func Exists(key string) (bool, error) { - c := pool.Get() +func Exist(key string) (bool, error) { + c := GetConn() defer c.Close() return redisdb.Bool(c.Do("EXISTS", key)) @@ -14,8 +14,8 @@ func Exists(key string) (bool, error) { } //hexists -func HExists(key string) (bool, error) { - c := pool.Get() +func HExist(key string) (bool, error) { + c := GetConn() defer c.Close() return redisdb.Bool(c.Do("HEXISTS", key)) diff --git a/hash.go b/hash.go index f6d2967..fafb9f8 100644 --- a/hash.go +++ b/hash.go @@ -112,8 +112,8 @@ func HGetList(key string) ([]string, error) { // var info map[string]string var all []string - if len(values) > 0 { - for key, item := range values { + if len(reply) > 0 { + for key, item := range reply { if (key+1)%2 == 0 { //只处理偶数位 all = append(all, string(item)) } diff --git a/init.go b/init.go index 6092660..26ebbe1 100644 --- a/init.go +++ b/init.go @@ -3,20 +3,30 @@ package redis import ( "log" "time" + redisdb "github.com/gomodule/redigo/redis" ) // 定义redis链接池 var Pool *redisdb.Pool var redisServerUrl string = "127.0.0.1:6379" + // func init() { // if Pool == nil { // RedisInit() // } // } +func Conn(server_url ...string) { + var url string = "127.0.0.1:6379" + if len(server_url) > 0 { + url = server_url[0] + } + RedisInit(url) +} + // 初始化redis链接池 -func RedisInit(serverUrl string,max ...int) { +func RedisInit(serverUrl string, max ...int) { var MaxActive, MaxIdle int if len(max) > 0 { diff --git a/set.go b/set.go index 5714dd0..6941fc9 100644 --- a/set.go +++ b/set.go @@ -1,8 +1,6 @@ package redis import ( - "errors" - redisdb "github.com/gomodule/redigo/redis" ) @@ -93,83 +91,3 @@ func SMembers(key string) ([]byte, error) { return reply, err } - -func ZAdd(key, score, member string) (int64, error) { - c := GetConn() - - reply, err := c.Do("ZADD", key, score, member) - CloseConn(c) - - if err != nil { - return 0, err - } else { - return redisdb.Int64(reply, nil) - } - -} - -func ZIncrBy(key, inc, member string) (int64, error) { - c := GetConn() - - reply, err := c.Do("ZINCRBY", key, inc, member) - CloseConn(c) - - if err != nil { - return 0, err - } else { - return redisdb.Int64(reply, nil) - } - -} - -func ZRange(key string, start, stop int64) ([]map[string]string, error) { - c := GetConn() - defer CloseConn(c) - values, err := redisdb.Values(c.Do("ZRANGE", key, start, stop, "WITHSCORES")) - if err != nil { - return nil, err - } - if len(values)%2 != 0 { - return nil, errors.New("redigo: ZRange expects even number of values result") - } - var l []map[string]string - for i := 0; i < len(values); i += 2 { - key, okKey := values[i].([]byte) - value, okValue := values[i+1].([]byte) - if !okKey || !okValue { - return nil, errors.New("redigo: ZRange key not a bulk string value") - } - l = append(l, map[string]string{ - "member": string(key), - "score": string(value), - }) - } - - return l, nil - -} - -func ZScan(key, field string, len int64) ([]string, error) { - c := GetConn() - defer CloseConn(c) - - zscanResult, err := redisdb.Values(c.Do("ZSCAN", key, "0", "MATCH", field, "COUNT", len)) - - if err != nil { - return nil, err - } - - var cursor int - var membersAndScores []string - var member []string - redisdb.Scan(zscanResult, &cursor, &membersAndScores) - - for i, v := range membersAndScores { - if i%2 == 0 { - member = append(member, v) - } - } - - return member, nil - -} diff --git a/zset.go b/zset.go new file mode 100644 index 0000000..e09964b --- /dev/null +++ b/zset.go @@ -0,0 +1,211 @@ +package redis + +import ( + "errors" + + redisdb "github.com/gomodule/redigo/redis" +) + +/** + * 集合成员数量 + */ +func ZCard(key interface{}) (int64, error) { + c := GetConn() + + reply, err := c.Do("ZCARD", key) + + CloseConn(c) + + if err != nil { + return 0, err + } else { + return redisdb.Int64(reply, nil) + } +} + +/* + * 存值 + * key 域 + * member 成员 + * score 分值 + */ +func ZAdd(key string, score, member interface{}) (int64, error) { + c := GetConn() + + reply, err := c.Do("ZADD", key, score, member) + CloseConn(c) + if err != nil { + return 0, err + } else { + return redisdb.Int64(reply, nil) + } + +} + +/* + * 删除hash值 + */ +func ZRem(key, member interface{}) (int64, error) { + c := GetConn() + + reply, err := c.Do("ZREM", key, member) + + CloseConn(c) + + if err != nil { + return 0, err + } else { + return redisdb.Int64(reply, nil) + } +} + +/* + * 区间数量 + * key 域 + * min/max 最小/最大值 + */ +func ZCount(key string, min, max interface{}) (int64, error) { + c := GetConn() + + reply, err := c.Do("ZCOUNT", key, min, max) + CloseConn(c) + + if err != nil { + return 0, err + } else { + return redisdb.Int64(reply, nil) + } + +} + +/* + * 自增 + * key 域 + * member 成员 + * inc 分值 + */ +func ZIncrBy(key string, inc, member interface{}) (int64, error) { + c := GetConn() + + reply, err := c.Do("ZINCRBY", key, inc, member) + CloseConn(c) + + if err != nil { + return 0, err + } else { + return redisdb.Int64(reply, nil) + } + +} + +func ZRange(key string, start, stop int64) ([]map[string]string, error) { + c := GetConn() + defer CloseConn(c) + values, err := redisdb.Values(c.Do("ZRANGE", key, start, stop, "WITHSCORES")) + if err != nil { + return nil, err + } + if len(values)%2 != 0 { + return nil, errors.New("redigo: ZRange expects even number of values result") + } + var l []map[string]string + for i := 0; i < len(values); i += 2 { + key, okKey := values[i].([]byte) + value, okValue := values[i+1].([]byte) + if !okKey || !okValue { + return nil, errors.New("redigo: ZRange key not a bulk string value") + } + l = append(l, map[string]string{ + "member": string(key), + "score": string(value), + }) + } + + return l, nil + +} + +func ZScan(key, field string, len int64) ([]string, error) { + c := GetConn() + defer CloseConn(c) + + zscanResult, err := redisdb.Values(c.Do("ZSCAN", key, "0", "MATCH", field, "COUNT", len)) + + if err != nil { + return nil, err + } + + var cursor int + var membersAndScores []string + var member []string + redisdb.Scan(zscanResult, &cursor, &membersAndScores) + + for i, v := range membersAndScores { + if i%2 == 0 { + member = append(member, v) + } + } + + return member, nil + +} + +/* + * 回有序集合中指定成员的索引(从小到大排名) + * key 域 + * member 成员 + */ +func ZRank(key string, member interface{}) (int64, error) { + c := GetConn() + + reply, err := c.Do("ZRANK", key, member) + + CloseConn(c) + + if err != nil { + return 0, err + } else { + return redisdb.Int64(reply, nil) + } + +} + +/* + * 有序集合中指定成员的索引(从大到小排名) + * key 域 + * member 成员 + */ +func ZRevrank(key string, member interface{}) (int64, error) { + c := GetConn() + + reply, err := c.Do("ZREVRANK", key, member) + + CloseConn(c) + + if err != nil { + return 0, err + } else { + return redisdb.Int64(reply, nil) + } + +} + +/* + * 指定成员的分数 + * key 域 + * member 成员 + */ +func ZScore(key string, member interface{}) (float64, error) { + c := GetConn() + + reply, err := c.Do("ZSCORE", key, member) + + CloseConn(c) + + if err != nil { + return 0, err + } else { + return redisdb.Float64(reply, nil) + } + +} diff --git a/zset_test.go b/zset_test.go new file mode 100644 index 0000000..b1816af --- /dev/null +++ b/zset_test.go @@ -0,0 +1,41 @@ +package redis + +import ( + "testing" +) + +func Test_Zset(t *testing.T) { + Conn() + ret3, err := ZAdd("zset", 9, 9) + t.Log(ret3) + t.Log(err) + + ret3, err = ZCard("zset") + t.Log(ret3) + t.Log(err) + + // ret3, err = ZRem("zset", 9) + // t.Log(ret3) + // t.Log(err) + + ret3, err = ZCount("zset", 0, 100) + t.Log(ret3) + t.Log(err) + + ret3, err = ZIncrBy("zset", 10, 9) + t.Log(ret3) + t.Log(err) + + ret3, err = ZRank("zset", 9) + t.Log(ret3) + t.Log(err) + + ret3, err = ZRevrank("zset", 9) + t.Log(ret3) + t.Log(err) + + ret4, err := ZScore("zset", 9) + t.Log(ret4) + t.Log(err) + +}