standalone_redis.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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) GetStr(key string) string {
  55. return rc.redisClient.Get(context.TODO(), key).Val()
  56. }
  57. func (rc *StandaloneRedisClient) GetInt64(key string) (int64, error) {
  58. return rc.redisClient.Get(context.TODO(), key).Int64()
  59. }
  60. func (rc *StandaloneRedisClient) GetUInt64(key string) (uint64, error) {
  61. return rc.redisClient.Get(context.TODO(), key).Uint64()
  62. }
  63. func (rc *StandaloneRedisClient) RedisBytes(key string) (data []byte, err error) {
  64. data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
  65. return
  66. }
  67. func (rc *StandaloneRedisClient) RedisString(key string) (data string, err error) {
  68. data, err = rc.redisClient.Get(context.TODO(), key).Result()
  69. return
  70. }
  71. func (rc *StandaloneRedisClient) RedisInt(key string) (data int, err error) {
  72. data, err = rc.redisClient.Get(context.TODO(), key).Int()
  73. return
  74. }
  75. func (rc *StandaloneRedisClient) Put(key string, val interface{}, timeout time.Duration) error {
  76. var err error
  77. err = rc.redisClient.SetEX(context.TODO(), key, val, timeout).Err()
  78. if err != nil {
  79. return err
  80. }
  81. err = rc.redisClient.HSet(context.TODO(), DefaultKey, key, true).Err()
  82. return err
  83. }
  84. func (rc *StandaloneRedisClient) SetNX(key string, val interface{}, timeout time.Duration) bool {
  85. result, err := rc.redisClient.SetNX(context.TODO(), key, val, timeout).Result()
  86. if err != nil {
  87. return false
  88. }
  89. return result
  90. }
  91. func (rc *StandaloneRedisClient) Delete(key string) error {
  92. var err error
  93. err = rc.redisClient.Del(context.TODO(), key).Err()
  94. if err != nil {
  95. return err
  96. }
  97. err = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  98. return err
  99. }
  100. func (rc *StandaloneRedisClient) IsExist(key string) bool {
  101. result, err := rc.redisClient.Exists(context.TODO(), key).Result()
  102. if err != nil {
  103. return false
  104. }
  105. if result == 0 {
  106. _ = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
  107. return false
  108. }
  109. return true
  110. }
  111. func (rc *StandaloneRedisClient) LPush(key string, val interface{}) error {
  112. data, _ := json.Marshal(val)
  113. err := rc.redisClient.LPush(context.TODO(), key, data).Err()
  114. return err
  115. }
  116. func (rc *StandaloneRedisClient) Brpop(key string, callback func([]byte)) {
  117. values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
  118. if err != nil {
  119. return
  120. }
  121. if len(values) < 2 {
  122. fmt.Println("assert is wrong")
  123. return
  124. }
  125. callback([]byte(values[1]))
  126. }
  127. func (rc *StandaloneRedisClient) GetRedisTTL(key string) time.Duration {
  128. value, err := rc.redisClient.TTL(context.TODO(), key).Result()
  129. if err != nil {
  130. return 0
  131. }
  132. return value
  133. }
  134. func (rc *StandaloneRedisClient) Incrby(key string, num int) (interface{}, error) {
  135. return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
  136. }
  137. func (rc *StandaloneRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
  138. newArgs := []interface{}{commandName}
  139. newArgs = append(newArgs, args...)
  140. return rc.redisClient.Do(context.TODO(), newArgs...).Result()
  141. }