package redisrpc import ( "encoding/json" "log" "github.com/golang/protobuf/proto" ) //设置 func SAdd(key, field string, url ...string) (int64, error) { conn, _, err := Conn(url...) if err != nil { return 0, err } defer conn.Close() req := &SSetRequest{proto.String(key), proto.String(field), nil} res := &SSetResponse{} err = conn.SAdd(req, res) if err != nil { return 0, err } return res.GetRet(), nil } //删除 func SRem(key string, field string, url ...string) (int64, error) { conn, _, err := Conn(url...) if err != nil { return 0, err } defer conn.Close() req := &SSetRequest{proto.String(key), proto.String(field), nil} res := &SSetResponse{} err = conn.SRem(req, res) if err != nil { return 0, err } return res.GetRet(), nil } /** * 全部 */ func SIsmember(key string, field string, url ...string) (int64, error) { conn, _, err := Conn(url...) if err != nil { return 0, err } defer conn.Close() req := &SSetRequest{proto.String(key), proto.String(field), nil} res := &SSetResponse{} err = conn.SIsmember(req, res) if err != nil { return 0, err } return res.GetRet(), nil } /** * 集合成员数 */ func SCard(key string, url ...string) (int64, error) { conn, _, err := Conn(url...) if err != nil { return 0, err } defer conn.Close() req := &SMembersRequest{proto.String(key), nil} res := &SSetResponse{} err = conn.SCard(req, res) if err != nil { return 0, err } return res.GetRet(), nil } //设置 func ZAdd(key, score, member string, url ...string) (int64, error) { conn, _, err := Conn(url...) if err != nil { return 0, err } defer conn.Close() req := &ZSetRequest{proto.String(key), proto.String(score), proto.String(member), nil} res := &ZSetResponse{} err = conn.ZAdd(req, res) if err != nil { return 0, err } return res.GetRet(), nil } //递增 func ZIncrBy(key, inc, member string, url ...string) (int64, error) { conn, _, err := Conn(url...) if err != nil { return 0, err } defer conn.Close() req := &ZSetRequest{proto.String(key), proto.String(inc), proto.String(member), nil} res := &ZSetResponse{} err = conn.ZIncrBy(req, res) if err != nil { return 0, err } return res.GetRet(), nil } /** * 集合成员 */ func ZRange(key string, start, stop int64, url ...string) ([]map[string]string, error) { conn, _, err := Conn(url...) if err != nil { return nil, err } defer conn.Close() req := &ZRangeRequest{proto.String(key), proto.Int64(start), proto.Int64(stop), nil} res := &HGetListResponse{} err = conn.ZRange(req, res) if err != nil { return nil, err } value := res.GetList() var list []map[string]string err = json.Unmarshal(value, &list) if err != nil { log.Println("json unmarshal error:", err) return nil, err } return list, nil } /** * 集合成员 */ func ZScan(key, field string, len int64, url ...string) ([]string, error) { conn, _, err := Conn(url...) if err != nil { return nil, err } defer conn.Close() req := &ZScanRequest{proto.String(key), proto.String(field), proto.Int64(len), nil} res := &HGetListResponse{} err = conn.ZScan(req, res) if err != nil { return nil, err } value := res.GetList() var list []string err = json.Unmarshal(value, &list) if err != nil { log.Println("json unmarshal error:", err) return nil, err } return list, nil }