gredis_redis.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/text/gstr"
  13. )
  14. // Redis client.
  15. type Redis struct {
  16. config *Config
  17. localAdapter
  18. localGroup
  19. }
  20. type (
  21. localGroup struct {
  22. localGroupGeneric
  23. localGroupHash
  24. localGroupList
  25. localGroupPubSub
  26. localGroupScript
  27. localGroupSet
  28. localGroupSortedSet
  29. localGroupString
  30. }
  31. localAdapter = Adapter
  32. localGroupGeneric = IGroupGeneric
  33. localGroupHash = IGroupHash
  34. localGroupList = IGroupList
  35. localGroupPubSub = IGroupPubSub
  36. localGroupScript = IGroupScript
  37. localGroupSet = IGroupSet
  38. localGroupSortedSet = IGroupSortedSet
  39. localGroupString = IGroupString
  40. )
  41. const (
  42. errorNilRedis = `the Redis object is nil`
  43. )
  44. var (
  45. errorNilAdapter = gstr.Trim(gstr.Replace(`
  46. redis adapter is not set, missing configuration or adapter register?
  47. possible reference: https://github.com/gogf/gf/tree/master/contrib/nosql/redis
  48. `, "\n", ""))
  49. )
  50. // initGroup initializes the group object of redis.
  51. func (r *Redis) initGroup() *Redis {
  52. r.localGroup = localGroup{
  53. localGroupGeneric: r.localAdapter.GroupGeneric(),
  54. localGroupHash: r.localAdapter.GroupHash(),
  55. localGroupList: r.localAdapter.GroupList(),
  56. localGroupPubSub: r.localAdapter.GroupPubSub(),
  57. localGroupScript: r.localAdapter.GroupScript(),
  58. localGroupSet: r.localAdapter.GroupSet(),
  59. localGroupSortedSet: r.localAdapter.GroupSortedSet(),
  60. localGroupString: r.localAdapter.GroupString(),
  61. }
  62. return r
  63. }
  64. // SetAdapter changes the underlying adapter with custom adapter for current redis client.
  65. func (r *Redis) SetAdapter(adapter Adapter) {
  66. if r == nil {
  67. panic(gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis))
  68. }
  69. r.localAdapter = adapter
  70. }
  71. // GetAdapter returns the adapter that is set in current redis client.
  72. func (r *Redis) GetAdapter() Adapter {
  73. if r == nil {
  74. return nil
  75. }
  76. return r.localAdapter
  77. }
  78. // Conn retrieves and returns a connection object for continuous operations.
  79. // Note that you should call Close function manually if you do not use this connection any further.
  80. func (r *Redis) Conn(ctx context.Context) (Conn, error) {
  81. if r == nil {
  82. return nil, gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis)
  83. }
  84. if r.localAdapter == nil {
  85. return nil, gerror.NewCode(gcode.CodeNecessaryPackageNotImport, errorNilAdapter)
  86. }
  87. return r.localAdapter.Conn(ctx)
  88. }
  89. // Do send a command to the server and returns the received reply.
  90. // It uses json.Marshal for struct/slice/map type values before committing them to redis.
  91. func (r *Redis) Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) {
  92. if r == nil {
  93. return nil, gerror.NewCode(gcode.CodeInvalidParameter, errorNilRedis)
  94. }
  95. if r.localAdapter == nil {
  96. return nil, gerror.NewCodef(gcode.CodeMissingConfiguration, errorNilAdapter)
  97. }
  98. return r.localAdapter.Do(ctx, command, args...)
  99. }
  100. // MustConn performs as function Conn, but it panics if any error occurs internally.
  101. func (r *Redis) MustConn(ctx context.Context) Conn {
  102. c, err := r.Conn(ctx)
  103. if err != nil {
  104. panic(err)
  105. }
  106. return c
  107. }
  108. // MustDo performs as function Do, but it panics if any error occurs internally.
  109. func (r *Redis) MustDo(ctx context.Context, command string, args ...interface{}) *gvar.Var {
  110. v, err := r.Do(ctx, command, args...)
  111. if err != nil {
  112. panic(err)
  113. }
  114. return v
  115. }
  116. // Close closes current redis client, closes its connection pool and releases all its related resources.
  117. func (r *Redis) Close(ctx context.Context) error {
  118. if r == nil || r.localAdapter == nil {
  119. return nil
  120. }
  121. return r.localAdapter.Close(ctx)
  122. }