redis rpc服务, 提供redis操作方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
1.7 KiB

  1. package redisrpc
  2. import (
  3. "encoding/json"
  4. "log"
  5. "github.com/golang/protobuf/proto"
  6. )
  7. //头部增加
  8. func LLpush(key, field string, url ...string) (int64, error) {
  9. conn, _, err := Conn(url...)
  10. if err != nil {
  11. return 0, err
  12. }
  13. defer conn.Close()
  14. req := &LSetRequest{proto.String(key), proto.String(field), nil}
  15. res := &LSetResponse{}
  16. err = conn.LLpush(req, res)
  17. if err != nil {
  18. return 0, err
  19. }
  20. return res.GetRet(), nil
  21. }
  22. //尾部增加
  23. func LRpush(key, field string, url ...string) (int64, error) {
  24. conn, _, err := Conn(url...)
  25. if err != nil {
  26. return 0, err
  27. }
  28. defer conn.Close()
  29. req := &LSetRequest{proto.String(key), proto.String(field), nil}
  30. res := &LSetResponse{}
  31. err = conn.LRpush(req, res)
  32. if err != nil {
  33. return 0, err
  34. }
  35. return res.GetRet(), nil
  36. }
  37. /**
  38. * 全部
  39. */
  40. func LRange(key string, start, stop int64, url ...string) ([]string, error) {
  41. conn, _, err := Conn(url...)
  42. if err != nil {
  43. return []string{}, err
  44. }
  45. defer conn.Close()
  46. req := &LRangeRequest{proto.String(key), proto.Int64(start), proto.Int64(stop), nil}
  47. res := &HGetListResponse{}
  48. err = conn.LRange(req, res)
  49. if err != nil {
  50. return []string{}, err
  51. }
  52. value := res.GetList()
  53. var list []string
  54. err = json.Unmarshal(value, &list)
  55. if err != nil {
  56. log.Println("json unmarshal error:", err)
  57. return []string{}, err
  58. }
  59. return list, nil
  60. }
  61. //长度
  62. func LLen(key string, url ...string) (int64, error) {
  63. conn, _, err := Conn(url...)
  64. if err != nil {
  65. return 0, err
  66. }
  67. defer conn.Close()
  68. req := &LLenRequest{proto.String(key), nil}
  69. res := &LSetResponse{}
  70. err = conn.LLen(req, res)
  71. if err != nil {
  72. return 0, err
  73. }
  74. return res.GetRet(), nil
  75. }