gredis_redis.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/gogf/gf/v2/container/gvar"
  10. "github.com/gogf/gf/v2/errors/gcode"
  11. "github.com/gogf/gf/v2/errors/gerror"
  12. "github.com/gogf/gf/v2/internal/intlog"
  13. )
  14. // Redis client.
  15. type Redis struct {
  16. adapter Adapter
  17. }
  18. const (
  19. errorNilRedis = `the Redis object is nil`
  20. )
  21. // SetAdapter sets custom adapter for current redis client.
  22. func (r *Redis) SetAdapter(adapter Adapter) {
  23. if r == nil {
  24. return
  25. }
  26. r.adapter = adapter
  27. }
  28. // GetAdapter returns the adapter that is set in current redis client.
  29. func (r *Redis) GetAdapter() Adapter {
  30. if r == nil {
  31. return nil
  32. }
  33. return r.adapter
  34. }
  35. // Conn retrieves and returns a connection object for continuous operations.
  36. // Note that you should call Close function manually if you do not use this connection any further.
  37. func (r *Redis) Conn(ctx context.Context) (*RedisConn, error) {
  38. if r == nil {
  39. return nil, gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis)
  40. }
  41. if r.adapter == nil {
  42. return nil, gerror.NewCodef(
  43. gcode.CodeMissingConfiguration,
  44. `redis adapter not initialized, missing configuration or adapter register?`,
  45. )
  46. }
  47. conn, err := r.adapter.Conn(ctx)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return &RedisConn{
  52. conn: conn,
  53. redis: r,
  54. }, nil
  55. }
  56. // Do send a command to the server and returns the received reply.
  57. // It uses json.Marshal for struct/slice/map type values before committing them to redis.
  58. func (r *Redis) Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) {
  59. if r == nil {
  60. return nil, gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis)
  61. }
  62. conn, err := r.Conn(ctx)
  63. if err != nil {
  64. return nil, err
  65. }
  66. defer func() {
  67. if closeErr := conn.Close(ctx); closeErr != nil {
  68. intlog.Errorf(ctx, `%+v`, closeErr)
  69. }
  70. }()
  71. return conn.Do(ctx, command, args...)
  72. }
  73. // MustConn performs as function Conn, but it panics if any error occurs internally.
  74. func (r *Redis) MustConn(ctx context.Context) *RedisConn {
  75. c, err := r.Conn(ctx)
  76. if err != nil {
  77. panic(err)
  78. }
  79. return c
  80. }
  81. // MustDo performs as function Do, but it panics if any error occurs internally.
  82. func (r *Redis) MustDo(ctx context.Context, command string, args ...interface{}) *gvar.Var {
  83. v, err := r.Do(ctx, command, args...)
  84. if err != nil {
  85. panic(err)
  86. }
  87. return v
  88. }
  89. // Close closes current redis client, closes its connection pool and releases all its related resources.
  90. func (r *Redis) Close(ctx context.Context) error {
  91. if r == nil {
  92. return gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis)
  93. }
  94. return r.adapter.Close(ctx)
  95. }