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) } } /* * 指定成员的分数, 返回int64 * key 域 * member 成员 */ func ZScoreInt64(key string, member interface{}) (int64, error) { c := GetConn() reply, err := c.Do("ZSCORE", key, member) CloseConn(c) if err != nil { return 0, err } else { return redisdb.Int64(reply, nil) } }