gredis_conn.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gredis
  7. import (
  8. "context"
  9. "github.com/gomodule/redigo/redis"
  10. "reflect"
  11. "time"
  12. "github.com/gogf/gf/container/gvar"
  13. "github.com/gogf/gf/errors/gcode"
  14. "github.com/gogf/gf/errors/gerror"
  15. "github.com/gogf/gf/internal/json"
  16. "github.com/gogf/gf/os/gtime"
  17. "github.com/gogf/gf/util/gconv"
  18. )
  19. // Do sends a command to the server and returns the received reply.
  20. // It uses json.Marshal for struct/slice/map type values before committing them to redis.
  21. // The timeout overrides the read timeout set when dialing the connection.
  22. func (c *Conn) do(timeout time.Duration, commandName string, args ...interface{}) (reply interface{}, err error) {
  23. var (
  24. reflectValue reflect.Value
  25. reflectKind reflect.Kind
  26. )
  27. for k, v := range args {
  28. reflectValue = reflect.ValueOf(v)
  29. reflectKind = reflectValue.Kind()
  30. if reflectKind == reflect.Ptr {
  31. reflectValue = reflectValue.Elem()
  32. reflectKind = reflectValue.Kind()
  33. }
  34. switch reflectKind {
  35. case
  36. reflect.Struct,
  37. reflect.Map,
  38. reflect.Slice,
  39. reflect.Array:
  40. // Ignore slice type of: []byte.
  41. if _, ok := v.([]byte); !ok {
  42. if args[k], err = json.Marshal(v); err != nil {
  43. return nil, err
  44. }
  45. }
  46. }
  47. }
  48. if timeout > 0 {
  49. conn, ok := c.Conn.(redis.ConnWithTimeout)
  50. if !ok {
  51. return gvar.New(nil), gerror.NewCode(gcode.CodeNotSupported, `current connection does not support "ConnWithTimeout"`)
  52. }
  53. return conn.DoWithTimeout(timeout, commandName, args...)
  54. }
  55. timestampMilli1 := gtime.TimestampMilli()
  56. reply, err = c.Conn.Do(commandName, args...)
  57. timestampMilli2 := gtime.TimestampMilli()
  58. // Tracing.
  59. c.addTracingItem(&tracingItem{
  60. err: err,
  61. commandName: commandName,
  62. arguments: args,
  63. costMilli: timestampMilli2 - timestampMilli1,
  64. })
  65. return
  66. }
  67. // Ctx is a channing function which sets the context for next operation.
  68. func (c *Conn) Ctx(ctx context.Context) *Conn {
  69. c.ctx = ctx
  70. return c
  71. }
  72. // Do sends a command to the server and returns the received reply.
  73. // It uses json.Marshal for struct/slice/map type values before committing them to redis.
  74. func (c *Conn) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
  75. return c.do(0, commandName, args...)
  76. }
  77. // DoWithTimeout sends a command to the server and returns the received reply.
  78. // The timeout overrides the read timeout set when dialing the connection.
  79. func (c *Conn) DoWithTimeout(timeout time.Duration, commandName string, args ...interface{}) (reply interface{}, err error) {
  80. return c.do(timeout, commandName, args...)
  81. }
  82. // DoVar retrieves and returns the result from command as gvar.Var.
  83. func (c *Conn) DoVar(commandName string, args ...interface{}) (*gvar.Var, error) {
  84. return resultToVar(c.Do(commandName, args...))
  85. }
  86. // DoVarWithTimeout retrieves and returns the result from command as gvar.Var.
  87. // The timeout overrides the read timeout set when dialing the connection.
  88. func (c *Conn) DoVarWithTimeout(timeout time.Duration, commandName string, args ...interface{}) (*gvar.Var, error) {
  89. return resultToVar(c.DoWithTimeout(timeout, commandName, args...))
  90. }
  91. // ReceiveVar receives a single reply as gvar.Var from the Redis server.
  92. func (c *Conn) ReceiveVar() (*gvar.Var, error) {
  93. return resultToVar(c.Receive())
  94. }
  95. // ReceiveVarWithTimeout receives a single reply as gvar.Var from the Redis server.
  96. // The timeout overrides the read timeout set when dialing the connection.
  97. func (c *Conn) ReceiveVarWithTimeout(timeout time.Duration) (*gvar.Var, error) {
  98. conn, ok := c.Conn.(redis.ConnWithTimeout)
  99. if !ok {
  100. return gvar.New(nil), gerror.NewCode(gcode.CodeNotSupported, `current connection does not support "ConnWithTimeout"`)
  101. }
  102. return resultToVar(conn.ReceiveWithTimeout(timeout))
  103. }
  104. // resultToVar converts redis operation result to gvar.Var.
  105. func resultToVar(result interface{}, err error) (*gvar.Var, error) {
  106. if err == nil {
  107. if result, ok := result.([]byte); ok {
  108. return gvar.New(string(result)), err
  109. }
  110. // It treats all returned slice as string slice.
  111. if result, ok := result.([]interface{}); ok {
  112. return gvar.New(gconv.Strings(result)), err
  113. }
  114. }
  115. return gvar.New(result), err
  116. }