You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.go 4.7 kB

3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package redis_client
  2. import (
  3. "code.gitea.io/gitea/modules/labelmsg"
  4. "fmt"
  5. "github.com/gomodule/redigo/redis"
  6. "math"
  7. "strconv"
  8. "time"
  9. )
  10. func Setex(key, value string, timeout time.Duration) (bool, error) {
  11. redisClient := labelmsg.Get()
  12. defer redisClient.Close()
  13. seconds := int(math.Floor(timeout.Seconds()))
  14. reply, err := redisClient.Do("SETEX", key, seconds, value)
  15. if err != nil {
  16. return false, err
  17. }
  18. if reply != "OK" {
  19. return false, nil
  20. }
  21. return true, nil
  22. }
  23. func Setnx(key, value string, timeout time.Duration) (bool, error) {
  24. redisClient := labelmsg.Get()
  25. defer redisClient.Close()
  26. seconds := int(math.Floor(timeout.Seconds()))
  27. reply, err := redisClient.Do("SET", key, value, "NX", "EX", seconds)
  28. if err != nil {
  29. return false, err
  30. }
  31. if reply != "OK" {
  32. return false, nil
  33. }
  34. return true, nil
  35. }
  36. func SETNX(conn redis.Conn, key, value string, seconds int) (bool, error) {
  37. reply, err := conn.Do("SET", key, value, "NX", "EX", seconds)
  38. return redis.Bool(reply, err)
  39. }
  40. func SET(conn redis.Conn, key, value string, seconds int) (bool, error) {
  41. reply, err := conn.Do("SETEX", key, seconds, value)
  42. return redis.Bool(reply, err)
  43. }
  44. func HSETNX(conn redis.Conn, key, subKey string, value interface{}) error {
  45. _, err := conn.Do("HSETNX", key, subKey, value)
  46. return err
  47. }
  48. func HGET(conn redis.Conn, key, subKey string) (interface{}, error) {
  49. return conn.Do("HGET", key, subKey)
  50. }
  51. func EXISTS(conn redis.Conn, key string) (bool, error) {
  52. reply, err := conn.Do("EXISTS", key)
  53. return redis.Bool(reply, err)
  54. }
  55. func HEXISTS(conn redis.Conn, key string, subKey string) (bool, error) {
  56. reply, err := conn.Do("HEXISTS", key, subKey)
  57. return redis.Bool(reply, err)
  58. }
  59. func EXPIRE(conn redis.Conn, key string, seconds int) error {
  60. _, err := conn.Do("EXPIRE", key, seconds)
  61. return err
  62. }
  63. func HINCRBY(conn redis.Conn, key, subKey string, value int) error {
  64. _, err := conn.Do("HINCRBY", key, subKey, value)
  65. return err
  66. }
  67. func GET(conn redis.Conn, key string) (interface{}, error) {
  68. return conn.Do("GET", key)
  69. }
  70. func Ttl(conn redis.Conn, key string) (int, error) {
  71. reply, err := conn.Do("TTL", key)
  72. if err != nil {
  73. return 0, err
  74. }
  75. n, _ := strconv.Atoi(fmt.Sprint(reply))
  76. return n, nil
  77. }
  78. func Get(key string) (string, error) {
  79. redisClient := labelmsg.Get()
  80. defer redisClient.Close()
  81. reply, err := redisClient.Do("GET", key)
  82. if err != nil {
  83. return "", err
  84. }
  85. if reply == nil {
  86. return "", err
  87. }
  88. s, _ := redis.String(reply, nil)
  89. return s, nil
  90. }
  91. func Del(key string) (int, error) {
  92. redisClient := labelmsg.Get()
  93. defer redisClient.Close()
  94. reply, err := redisClient.Do("DEL", key)
  95. if err != nil {
  96. return 0, err
  97. }
  98. if reply == nil {
  99. return 0, err
  100. }
  101. s, _ := redis.Int(reply, nil)
  102. return s, nil
  103. }
  104. func TTL(key string) (int, error) {
  105. redisClient := labelmsg.Get()
  106. defer redisClient.Close()
  107. reply, err := redisClient.Do("TTL", key)
  108. if err != nil {
  109. return 0, err
  110. }
  111. n, _ := strconv.Atoi(fmt.Sprint(reply))
  112. return n, nil
  113. }
  114. func IncrBy(key string, n int64) (int64, error) {
  115. redisClient := labelmsg.Get()
  116. defer redisClient.Close()
  117. reply, err := redisClient.Do("INCRBY", key, n)
  118. if err != nil {
  119. return 0, err
  120. }
  121. i, err := strconv.ParseInt(fmt.Sprint(reply), 10, 64)
  122. return i, nil
  123. }
  124. func Expire(key string, expireTime time.Duration) error {
  125. redisClient := labelmsg.Get()
  126. defer redisClient.Close()
  127. _, err := redisClient.Do("EXPIRE", key, int64(expireTime.Seconds()))
  128. if err != nil {
  129. return err
  130. }
  131. return nil
  132. }
  133. //GetInt64 get redis value by Get(key)
  134. //and then parse the value to int64
  135. //return {isExist(bool)} {value(int64)} {error(error)}
  136. func GetInt64(key string) (bool, int64, error) {
  137. str, err := Get(key)
  138. if err != nil {
  139. return false, 0, err
  140. }
  141. if str == "" {
  142. return false, 0, nil
  143. }
  144. i, err := strconv.ParseInt(str, 10, 64)
  145. if err != nil {
  146. return false, 0, err
  147. }
  148. return true, i, nil
  149. }
  150. func ZAdd(key, value string, score float64) error {
  151. redisClient := labelmsg.Get()
  152. defer redisClient.Close()
  153. _, err := redisClient.Do("ZADD", key, score, value)
  154. if err != nil {
  155. return err
  156. }
  157. return nil
  158. }
  159. func ZRangeByScore(key string, min, max float64) ([]string, error) {
  160. redisClient := labelmsg.Get()
  161. defer redisClient.Close()
  162. reply, err := redisClient.Do("ZRANGEBYSCORE", key, min, max)
  163. if err != nil {
  164. return nil, err
  165. }
  166. if reply == nil {
  167. return nil, err
  168. }
  169. s, _ := redis.Strings(reply, nil)
  170. return s, nil
  171. }
  172. func ZRemRangeByScore(key string, min, max float64) error {
  173. redisClient := labelmsg.Get()
  174. defer redisClient.Close()
  175. _, err := redisClient.Do("ZREMRANGEBYSCORE", key, min, max)
  176. if err != nil {
  177. return err
  178. }
  179. return nil
  180. }