standalone_redis.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. type StandaloneRedisClient struct {
  12. redisClient *redis.Client
  13. }
  14. func InitStandaloneRedis(config string) (standaloneRedis *StandaloneRedisClient, err error) {
  15. var cf map[string]string
  16. err = json.Unmarshal([]byte(config), &cf)
  17. if err != nil {
  18. return
  19. }
  20. if _, ok := cf["conn"]; !ok {
  21. err = errors.New("config has no conn key")
  22. return
  23. }
  24. dbNum := 0
  25. if _, ok := cf["dbNum"]; ok {
  26. dbNum, err = strconv.Atoi(cf["dbNum"])
  27. if err != nil {
  28. return
  29. }
  30. }
  31. if _, ok := cf["password"]; !ok {
  32. cf["password"] = ""
  33. }
  34. client := redis.NewClient(&redis.Options{
  35. Addr: cf["conn"],
  36. Password: cf["password"],
  37. DB: dbNum,
  38. })
  39. _, err = client.Ping(context.TODO()).Result()
  40. if err != nil {
  41. err = errors.New("redis 链接失败:" + err.Error())
  42. return
  43. }
  44. standaloneRedis = &StandaloneRedisClient{redisClient: client}
  45. return
  46. }
  47. func (rc *StandaloneRedisClient) Get(key string) interface{} {
  48. data, err := rc.redisClient.Get(context.TODO(), key).Bytes()
  49. if err != nil {
  50. return nil
  51. }
  52. return data
  53. }
  54. func (rc *StandaloneRedisClient) RedisBytes(key string) (data []byte, err error) {
  55. data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
  56. return
  57. }
  58. func (rc *StandaloneRedisClient) RedisString(key string) (data string, err error) {
  59. data, err = rc.redisClient.Get(context.TODO(), key).Result()
  60. return
  61. }
  62. func (rc *StandaloneRedisClient) RedisInt(key string) (data int, err error) {
  63. data, err = rc.redisClient.Get(context.TODO(), key).Int()
  64. return
  65. }
  66. func (rc *StandaloneRedisClient) Put(key string, val interface{}, timeout time.Duration) error {
  67. var err error
  68. err = rc.redisClient.SetEX(context.TODO(), key, val, timeout).Err()
  69. if err != nil {
  70. return err
  71. }
  72. err = rc.redisClient.HSet(context.TODO(), DefaultKey, key, true).Err()
  73. return err
  74. }
  75. func (rc *StandaloneRedisClient) SetNX(key string, val interface{}, timeout time.Duration) bool {
  76. result, err := rc.redisClient.SetNX(context.TODO(), key, val, timeout).Result()
  77. if err != nil {
  78. return false
  79. }
  80. return result
  81. }
  82. func (rc *StandaloneRedisClient) Delete(key string) error {
  83. var err error
  84. err = rc.redisClient.Del(context.TODO(), key).Err()
  85. if err != nil {
  86. return err
  87. }
  88. err = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  89. return err
  90. }
  91. func (rc *StandaloneRedisClient) IsExist(key string) bool {
  92. result, err := rc.redisClient.Exists(context.TODO(), key).Result()
  93. if err != nil {
  94. return false
  95. }
  96. if result == 0 {
  97. _ = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  98. return false
  99. }
  100. return true
  101. }
  102. func (rc *StandaloneRedisClient) LPush(key string, val interface{}) error {
  103. data, _ := json.Marshal(val)
  104. err := rc.redisClient.LPush(context.TODO(), key, data).Err()
  105. return err
  106. }
  107. func (rc *StandaloneRedisClient) Brpop(key string, callback func([]byte)) {
  108. values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
  109. if err != nil {
  110. return
  111. }
  112. if len(values) < 2 {
  113. fmt.Println("assert is wrong")
  114. return
  115. }
  116. callback([]byte(values[1]))
  117. }
  118. func (rc *StandaloneRedisClient) GetRedisTTL(key string) time.Duration {
  119. value, err := rc.redisClient.TTL(context.TODO(), key).Result()
  120. if err != nil {
  121. return 0
  122. }
  123. return value
  124. }
  125. func (rc *StandaloneRedisClient) Incrby(key string, num int) (interface{}, error) {
  126. return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
  127. }
  128. func (rc *StandaloneRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
  129. newArgs := []interface{}{commandName}
  130. newArgs = append(newArgs, args...)
  131. return rc.redisClient.Do(context.TODO(), newArgs...).Result()
  132. }