gredis_adapter.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. )
  11. // Adapter is an interface for universal redis operations.
  12. type Adapter interface {
  13. AdapterGroup
  14. // Do send a command to the server and returns the received reply.
  15. // It uses json.Marshal for struct/slice/map type values before committing them to redis.
  16. Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error)
  17. // Conn retrieves and returns a connection object for continuous operations.
  18. // Note that you should call Close function manually if you do not use this connection any further.
  19. Conn(ctx context.Context) (conn Conn, err error)
  20. // Close closes current redis client, closes its connection pool and releases all its related resources.
  21. Close(ctx context.Context) (err error)
  22. }
  23. // Conn is an interface of a connection from universal redis client.
  24. type Conn interface {
  25. ConnCommand
  26. // Do send a command to the server and returns the received reply.
  27. // It uses json.Marshal for struct/slice/map type values before committing them to redis.
  28. Do(ctx context.Context, command string, args ...interface{}) (result *gvar.Var, err error)
  29. // Close puts the connection back to connection pool.
  30. Close(ctx context.Context) (err error)
  31. }
  32. // AdapterGroup is an interface managing group operations for redis.
  33. type AdapterGroup interface {
  34. GroupGeneric() IGroupGeneric
  35. GroupHash() IGroupHash
  36. GroupList() IGroupList
  37. GroupPubSub() IGroupPubSub
  38. GroupScript() IGroupScript
  39. GroupSet() IGroupSet
  40. GroupSortedSet() IGroupSortedSet
  41. GroupString() IGroupString
  42. }
  43. // ConnCommand is an interface managing some operations bound to certain connection.
  44. type ConnCommand interface {
  45. // Subscribe subscribes the client to the specified channels.
  46. // https://redis.io/commands/subscribe/
  47. Subscribe(ctx context.Context, channel string, channels ...string) ([]*Subscription, error)
  48. // PSubscribe subscribes the client to the given patterns.
  49. //
  50. // Supported glob-style patterns:
  51. // - h?llo subscribes to hello, hallo and hxllo
  52. // - h*llo subscribes to hllo and heeeello
  53. // - h[ae]llo subscribes to hello and hallo, but not hillo
  54. //
  55. // Use \ to escape special characters if you want to match them verbatim.
  56. //
  57. // https://redis.io/commands/psubscribe/
  58. PSubscribe(ctx context.Context, pattern string, patterns ...string) ([]*Subscription, error)
  59. // ReceiveMessage receives a single message of subscription from the Redis server.
  60. ReceiveMessage(ctx context.Context) (*Message, error)
  61. // Receive receives a single reply as gvar.Var from the Redis server.
  62. Receive(ctx context.Context) (result *gvar.Var, err error)
  63. }