standalone_redis.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package redis
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/go-redis/redis/v8"
  8. "strconv"
  9. "time"
  10. )
  11. // StandaloneRedisClient
  12. // @Description: 单机redis客户端
  13. type StandaloneRedisClient struct {
  14. redisClient *redis.Client
  15. }
  16. func InitStandaloneRedis(config string) (standaloneRedis *StandaloneRedisClient, err error) {
  17. var cf map[string]string
  18. err = json.Unmarshal([]byte(config), &cf)
  19. if err != nil {
  20. return
  21. }
  22. //if _, ok := cf["key"]; !ok {
  23. // cf["key"] = DefaultKey
  24. //}
  25. if _, ok := cf["conn"]; !ok {
  26. err = errors.New("config has no conn key")
  27. return
  28. }
  29. // db库
  30. dbNum := 0
  31. // 如果指定了db库
  32. if _, ok := cf["dbNum"]; ok {
  33. dbNum, err = strconv.Atoi(cf["dbNum"])
  34. if err != nil {
  35. return
  36. }
  37. }
  38. // 密码
  39. if _, ok := cf["password"]; !ok {
  40. cf["password"] = ""
  41. }
  42. client := redis.NewClient(&redis.Options{
  43. Addr: cf["conn"],
  44. Password: cf["password"],
  45. DB: dbNum,
  46. //PoolSize: 10, //连接池最大socket连接数,默认为10倍CPU数, 10 * runtime.NumCPU(暂不配置)
  47. })
  48. _, err = client.Ping(context.TODO()).Result()
  49. if err != nil {
  50. err = errors.New("redis 链接失败:" + err.Error())
  51. return
  52. }
  53. standaloneRedis = &StandaloneRedisClient{redisClient: client}
  54. return
  55. }
  56. // Get
  57. // @Description: 根据key获取数据(其实是返回的字节编码)
  58. // @receiver rc
  59. // @param key
  60. // @return interface{}
  61. func (rc *StandaloneRedisClient) Get(key string) interface{} {
  62. data, err := rc.redisClient.Get(context.TODO(), key).Bytes()
  63. if err != nil {
  64. return nil
  65. }
  66. return data
  67. }
  68. // RedisBytes
  69. // @Description: 根据key获取字节编码数据
  70. // @receiver rc
  71. // @param key
  72. // @return data
  73. // @return err
  74. func (rc *StandaloneRedisClient) RedisBytes(key string) (data []byte, err error) {
  75. data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
  76. return
  77. }
  78. // RedisString
  79. // @Description: 根据key获取字符串数据
  80. // @receiver rc
  81. // @param key
  82. // @return data
  83. // @return err
  84. func (rc *StandaloneRedisClient) RedisString(key string) (data string, err error) {
  85. data, err = rc.redisClient.Get(context.TODO(), key).Result()
  86. return
  87. }
  88. // RedisInt
  89. // @Description: 根据key获取int数据
  90. // @receiver rc
  91. // @param key
  92. // @return data
  93. // @return err
  94. func (rc *StandaloneRedisClient) RedisInt(key string) (data int, err error) {
  95. data, err = rc.redisClient.Get(context.TODO(), key).Int()
  96. return
  97. }
  98. // Put
  99. // @Description: put一个数据到redis
  100. // @receiver rc
  101. // @param key
  102. // @param val
  103. // @param timeout
  104. // @return error
  105. func (rc *StandaloneRedisClient) Put(key string, val interface{}, timeout time.Duration) error {
  106. var err error
  107. err = rc.redisClient.SetEX(context.TODO(), key, val, timeout).Err()
  108. if err != nil {
  109. return err
  110. }
  111. err = rc.redisClient.HSet(context.TODO(), DefaultKey, key, true).Err()
  112. return err
  113. }
  114. // SetNX
  115. // @Description: 设置一个会过期时间的值
  116. // @receiver rc
  117. // @param key
  118. // @param val
  119. // @param timeout
  120. // @return bool
  121. func (rc *StandaloneRedisClient) SetNX(key string, val interface{}, timeout time.Duration) bool {
  122. result, err := rc.redisClient.SetNX(context.TODO(), key, val, timeout).Result()
  123. if err != nil {
  124. return false
  125. }
  126. return result
  127. }
  128. // Delete
  129. // @Description: 删除redis中的键值对
  130. // @receiver rc
  131. // @param key
  132. // @return error
  133. func (rc *StandaloneRedisClient) Delete(key string) error {
  134. var err error
  135. err = rc.redisClient.Del(context.TODO(), key).Err()
  136. if err != nil {
  137. return err
  138. }
  139. err = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  140. return err
  141. }
  142. // IsExist
  143. // @Description: 根据key判断是否写入缓存中
  144. // @receiver rc
  145. // @param key
  146. // @return bool
  147. func (rc *StandaloneRedisClient) IsExist(key string) bool {
  148. result, err := rc.redisClient.Exists(context.TODO(), key).Result()
  149. if err != nil {
  150. return false
  151. }
  152. if result == 0 {
  153. _ = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  154. return false
  155. }
  156. return true
  157. }
  158. // LPush
  159. // @Description: 写入list
  160. // @receiver rc
  161. // @param key
  162. // @param val
  163. // @return error
  164. func (rc *StandaloneRedisClient) LPush(key string, val interface{}) error {
  165. data, _ := json.Marshal(val)
  166. err := rc.redisClient.LPush(context.TODO(), key, data).Err()
  167. return err
  168. }
  169. // Brpop
  170. // @Description: 从list中读取
  171. // @receiver rc
  172. // @param key
  173. // @param callback
  174. func (rc *StandaloneRedisClient) Brpop(key string, callback func([]byte)) {
  175. values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
  176. if err != nil {
  177. return
  178. }
  179. if len(values) < 2 {
  180. fmt.Println("assert is wrong")
  181. return
  182. }
  183. callback([]byte(values[1]))
  184. }
  185. // GetRedisTTL
  186. // @Description: 获取key的过期时间
  187. // @receiver rc
  188. // @param key
  189. // @return time.Duration
  190. func (rc *StandaloneRedisClient) GetRedisTTL(key string) time.Duration {
  191. value, err := rc.redisClient.TTL(context.TODO(), key).Result()
  192. if err != nil {
  193. return 0
  194. }
  195. return value
  196. }
  197. // Incrby
  198. // @Description: 设置自增值
  199. // @receiver rc
  200. // @param key
  201. // @param num
  202. // @return interface{}
  203. // @return error
  204. func (rc *StandaloneRedisClient) Incrby(key string, num int) (interface{}, error) {
  205. return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
  206. }
  207. // Do
  208. // @Description: cmd执行redis命令
  209. // @receiver rc
  210. // @param commandName
  211. // @param args
  212. // @return reply
  213. // @return err
  214. func (rc *StandaloneRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
  215. newArgs := []interface{}{commandName}
  216. newArgs = append(newArgs, args...)
  217. return rc.redisClient.Do(context.TODO(), newArgs...).Result()
  218. }