client.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package redis
  2. import (
  3. "context"
  4. "time"
  5. cstring "github.com/cherry-game/cherry/extend/string"
  6. clog "github.com/cherry-game/cherry/logger"
  7. "github.com/go-redis/redis/v8"
  8. jsoniter "github.com/json-iterator/go"
  9. )
  10. type (
  11. IClient interface {
  12. ConfigID() string // 节点配置ID
  13. RedisID() string // redisID
  14. load(client *client) // 加载函数
  15. }
  16. client struct {
  17. *redis.Client
  18. redisID string
  19. }
  20. )
  21. func newClient(redisID string, options *redis.Options) *client {
  22. return &client{
  23. redisID: redisID,
  24. Client: redis.NewClient(options),
  25. }
  26. }
  27. func Context() context.Context {
  28. return context.Background()
  29. }
  30. func (p *client) IsNil() bool {
  31. return p.Client == nil
  32. }
  33. func (p *client) RedisID() string {
  34. return p.redisID
  35. }
  36. func (p *client) GetString(key string) (string, error) {
  37. return p.Get(Context(), key).Result()
  38. }
  39. func (p *client) GetInt32(key string) (int32, error) {
  40. val, err := p.GetString(key)
  41. if err != nil {
  42. return 0, err
  43. }
  44. return cstring.ToInt32D(val, 0), nil
  45. }
  46. func (p *client) GetInt64(key string) (int64, error) {
  47. cmd := p.Get(Context(), key)
  48. if cmd.Err() != nil {
  49. return 0, cmd.Err()
  50. }
  51. return cmd.Int64()
  52. }
  53. func (p *client) GetBytes(key string) ([]byte, error) {
  54. cmd := p.Get(Context(), key)
  55. if cmd.Err() != nil {
  56. return nil, cmd.Err()
  57. }
  58. return cmd.Bytes()
  59. }
  60. func (p *client) GetJSON(key string, value any) error {
  61. cmd := p.Get(Context(), key)
  62. if cmd.Err() != nil {
  63. return cmd.Err()
  64. }
  65. return jsoniter.UnmarshalFromString(cmd.Val(), value)
  66. }
  67. func (p *client) HGetString(key, field string) (string, error) {
  68. cmd := p.HGet(Context(), key, field)
  69. if cmd.Err() != nil {
  70. return "", cmd.Err()
  71. }
  72. return cmd.Result()
  73. }
  74. func (p *client) HGetInt64(key, field string) (int64, error) {
  75. cmd := p.HGet(Context(), key, field)
  76. if cmd.Err() != nil {
  77. return 0, cmd.Err()
  78. }
  79. return cmd.Int64()
  80. }
  81. func (p *client) HGetJSON(key, field string, value any) error {
  82. cmd := p.HGet(Context(), key, field)
  83. if cmd.Err() != nil {
  84. return cmd.Err()
  85. }
  86. return jsoniter.UnmarshalFromString(cmd.Val(), value)
  87. }
  88. func (p *client) HGetStringMap(key string, fields ...string) (map[string]string, error) {
  89. result, err := p.Client.HMGet(Context(), key).Result()
  90. if err != nil {
  91. return nil, err
  92. }
  93. m := map[string]string{}
  94. for i := 0; i < len(result)-1; i += 2 {
  95. k := cstring.ToString(result[i])
  96. v := cstring.ToString(result[i+1])
  97. m[k] = v
  98. }
  99. return m, nil
  100. }
  101. func (p *client) SetInt64(key string, value int64, expire time.Duration) bool {
  102. return p.SetAny(key, value, expire)
  103. }
  104. func (p *client) SetString(key string, value string, expire time.Duration) bool {
  105. return p.SetAny(key, value, expire)
  106. }
  107. func (p *client) SetBytes(key string, value []byte, expire time.Duration) bool {
  108. return p.SetAny(key, value, expire)
  109. }
  110. func (p *client) SetJSON(key string, value any, expire time.Duration) bool {
  111. str, err := jsoniter.MarshalToString(value)
  112. if err != nil {
  113. clog.Warnf("[SetJSON] marshal error. value = %v", value)
  114. return false
  115. }
  116. return p.SetAny(key, str, expire)
  117. }
  118. func (p *client) SetAny(key string, value any, expire time.Duration) bool {
  119. _, err := p.Set(Context(), key, value, expire).Result()
  120. if err != nil {
  121. clog.Warnf("[SetAny] result error. [redisID = %s, key = %s] [error = %v]", p.redisID, key, err)
  122. return false
  123. }
  124. return true
  125. }
  126. func (p *client) Exists(key string) bool {
  127. val, err := p.Client.Exists(Context(), key).Result()
  128. if err != nil {
  129. clog.Warnf("[Exists] result error. [redisID = %s, key = %s] [error = %v]", p.redisID, key, err)
  130. return false
  131. }
  132. return val == 1
  133. }
  134. func (p *client) Del(key ...string) bool {
  135. val, err := p.Client.Del(Context(), key...).Result()
  136. if err != nil {
  137. clog.Warnf("[Del] result error. [redisID = %s, key = %s] [error = %v]", p.redisID, key, err)
  138. return false
  139. }
  140. return val == 1
  141. }
  142. func (p *client) Scan(pattern string, cursor uint64, count int64) ([]string, uint64, error) {
  143. sc := p.Client.Scan(Context(), cursor, pattern, count)
  144. return sc.Result()
  145. }
  146. func (p *client) ListRange(key string, start, stop int64) ([]string, error) {
  147. val, err := p.LRange(Context(), key, start, stop).Result()
  148. if err != nil {
  149. clog.Warnf("[ListRange] result error. [redisID = %s, key = %s, start = %d, stop = %d] [error = %v]",
  150. p.redisID,
  151. key,
  152. start,
  153. stop,
  154. err,
  155. )
  156. return nil, err
  157. }
  158. return val, nil
  159. }
  160. func (p *client) HSetJSON(key, field string, value any) error {
  161. jsonBytes, err := jsoniter.Marshal(value)
  162. if err != nil {
  163. clog.Warnf("[HSetJSON] marshal error. [redisID = %s, key = %s, field = %v] [error = %v]",
  164. p.redisID,
  165. key,
  166. field,
  167. err,
  168. )
  169. return err
  170. }
  171. return p.HSet(Context(), key, field, jsonBytes).Err()
  172. }