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.

120 lines
1.8 KiB

3 years ago
3 years ago
  1. package redisrpc
  2. import (
  3. "github.com/golang/protobuf/proto"
  4. )
  5. /**
  6. * 使用用户名查询
  7. */
  8. func GetString(key string, url ...string) (string, error) {
  9. conn, _, err := Conn(url...)
  10. if err != nil {
  11. return "", err
  12. }
  13. defer conn.Close()
  14. req := &GetRequest{proto.String(key), nil}
  15. res := &GetStringResponse{}
  16. err = conn.Get(req, res)
  17. if err != nil {
  18. return "", err
  19. }
  20. return res.GetValue(), nil
  21. }
  22. //设置
  23. func Set(key, value string, ttl int64, url ...string) (string, error) {
  24. conn, _, err := Conn(url...)
  25. if err != nil {
  26. return "", err
  27. }
  28. defer conn.Close()
  29. req := &SetRequest{proto.String(key), proto.String(value), proto.Int64(ttl), nil}
  30. res := &SetResponse{}
  31. err = conn.Set(req, res)
  32. if err != nil {
  33. return "", err
  34. }
  35. return res.GetRet(), nil
  36. }
  37. //删除
  38. func Del(key string, url ...string) (int64, error) {
  39. conn, _, err := Conn(url...)
  40. if err != nil {
  41. return 0, err
  42. }
  43. defer conn.Close()
  44. req := &DelRequest{proto.String(key), nil}
  45. res := &DelResponse{}
  46. err = conn.Del(req, res)
  47. if err != nil {
  48. return 0, err
  49. }
  50. return res.GetRet(), nil
  51. }
  52. func Exists(key string, url ...string)(int64, error){
  53. conn, _, err := Conn(url...)
  54. if err != nil {
  55. return 0, err
  56. }
  57. defer conn.Close()
  58. req := &GetRequest{proto.String(key), nil}
  59. res := &DelResponse{}
  60. err = conn.Exists(req, res)
  61. if err != nil {
  62. return 0, err
  63. }
  64. return res.GetRet(), nil
  65. }
  66. //设置如果不存在
  67. func SetNx(key, value string, ttl int64, url ...string) (int64, error) {
  68. conn, _, err := Conn(url...)
  69. if err != nil {
  70. return 0, err
  71. }
  72. defer conn.Close()
  73. req := &SetRequest{proto.String(key), proto.String(value), proto.Int64(ttl), nil}
  74. res := &DelResponse{}
  75. err = conn.SetNx(req, res)
  76. if err != nil {
  77. return 0, err
  78. }
  79. return res.GetRet(), nil
  80. }