Browse Source

增加有序集合处理

master v1.1.0
guzeng 2 years ago
parent
commit
8dd1731b3f
6 changed files with 269 additions and 89 deletions
  1. +4
    -4
      exists.go
  2. +2
    -2
      hash.go
  3. +11
    -1
      init.go
  4. +0
    -82
      set.go
  5. +211
    -0
      zset.go
  6. +41
    -0
      zset_test.go

+ 4
- 4
exists.go View File

@ -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))


+ 2
- 2
hash.go View File

@ -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))
}


+ 11
- 1
init.go View File

@ -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 {


+ 0
- 82
set.go View File

@ -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
}

+ 211
- 0
zset.go View File

@ -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)
}
}

+ 41
- 0
zset_test.go View File

@ -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)
}

Loading…
Cancel
Save