standalone_redis.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package redis
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "strconv"
  8. "time"
  9. "github.com/go-redis/redis/v8"
  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. // SetEX
  129. // @Description: 设置一个会过期时间的值
  130. // @receiver rc
  131. // @param key
  132. // @param val
  133. // @param timeout
  134. // @return bool
  135. func (rc *StandaloneRedisClient) SetEX(key string, val interface{}, timeout time.Duration) (ok bool) {
  136. result, err := rc.redisClient.SetEX(context.TODO(), key, val, timeout).Result()
  137. if err != nil {
  138. return false
  139. }
  140. if result == "OK" {
  141. ok = true
  142. }
  143. return
  144. }
  145. // Delete
  146. // @Description: 删除redis中的键值对
  147. // @receiver rc
  148. // @param key
  149. // @return error
  150. func (rc *StandaloneRedisClient) Delete(key string) error {
  151. var err error
  152. err = rc.redisClient.Del(context.TODO(), key).Err()
  153. if err != nil {
  154. return err
  155. }
  156. err = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  157. return err
  158. }
  159. // IsExist
  160. // @Description: 根据key判断是否写入缓存中
  161. // @receiver rc
  162. // @param key
  163. // @return bool
  164. func (rc *StandaloneRedisClient) IsExist(key string) bool {
  165. result, err := rc.redisClient.Exists(context.TODO(), key).Result()
  166. if err != nil {
  167. return false
  168. }
  169. if result == 0 {
  170. _ = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  171. return false
  172. }
  173. return true
  174. }
  175. // LPush
  176. // @Description: 写入list
  177. // @receiver rc
  178. // @param key
  179. // @param val
  180. // @return error
  181. func (rc *StandaloneRedisClient) LPush(key string, val interface{}) error {
  182. data, _ := json.Marshal(val)
  183. err := rc.redisClient.LPush(context.TODO(), key, data).Err()
  184. return err
  185. }
  186. // Brpop
  187. // @Description: 从list中读取
  188. // @receiver rc
  189. // @param key
  190. // @param callback
  191. func (rc *StandaloneRedisClient) Brpop(key string, callback func([]byte)) {
  192. values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
  193. if err != nil {
  194. return
  195. }
  196. if len(values) < 2 {
  197. fmt.Println("assert is wrong")
  198. return
  199. }
  200. callback([]byte(values[1]))
  201. }
  202. // GetRedisTTL
  203. // @Description: 获取key的过期时间
  204. // @receiver rc
  205. // @param key
  206. // @return time.Duration
  207. func (rc *StandaloneRedisClient) GetRedisTTL(key string) time.Duration {
  208. value, err := rc.redisClient.TTL(context.TODO(), key).Result()
  209. if err != nil {
  210. return 0
  211. }
  212. return value
  213. }
  214. // Incrby
  215. // @Description: 设置自增值
  216. // @receiver rc
  217. // @param key
  218. // @param num
  219. // @return interface{}
  220. // @return error
  221. func (rc *StandaloneRedisClient) Incrby(key string, num int) (interface{}, error) {
  222. return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
  223. }
  224. // Do
  225. // @Description: cmd执行redis命令
  226. // @receiver rc
  227. // @param commandName
  228. // @param args
  229. // @return reply
  230. // @return err
  231. func (rc *StandaloneRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
  232. newArgs := []interface{}{commandName}
  233. newArgs = append(newArgs, args...)
  234. return rc.redisClient.Do(context.TODO(), newArgs...).Result()
  235. }