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 2.8 kB

3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 HSETNX(conn redis.Conn, key, subKey string, value interface{}) error {
  41. _, err := conn.Do("HSETNX", key, subKey, value)
  42. return err
  43. }
  44. func HGET(conn redis.Conn, key, subKey string) (interface{}, error) {
  45. return conn.Do("HGET", key, subKey)
  46. }
  47. func EXISTS(conn redis.Conn, key string) (bool, error) {
  48. reply, err := conn.Do("EXISTS", key)
  49. return redis.Bool(reply, err)
  50. }
  51. func HEXISTS(conn redis.Conn, key string, subKey string) (bool, error) {
  52. reply, err := conn.Do("HEXISTS", key, subKey)
  53. return redis.Bool(reply, err)
  54. }
  55. func Expire(conn redis.Conn, key string, seconds int) error {
  56. _, err := conn.Do("EXPIRE", key, seconds)
  57. return err
  58. }
  59. func HINCRBY(conn redis.Conn, key, subKey string, value int) error {
  60. _, err := conn.Do("HINCRBY", key, subKey, value)
  61. return err
  62. }
  63. func GET(conn redis.Conn, key string) (interface{}, error) {
  64. return conn.Do("GET", key)
  65. }
  66. func Ttl(conn redis.Conn, key string) (int, error) {
  67. reply, err := conn.Do("TTL", key)
  68. if err != nil {
  69. return 0, err
  70. }
  71. n, _ := strconv.Atoi(fmt.Sprint(reply))
  72. return n, nil
  73. }
  74. func Get(key string) (string, error) {
  75. redisClient := labelmsg.Get()
  76. defer redisClient.Close()
  77. reply, err := redisClient.Do("GET", key)
  78. if err != nil {
  79. return "", err
  80. }
  81. if reply == nil {
  82. return "", err
  83. }
  84. s, _ := redis.String(reply, nil)
  85. return s, nil
  86. }
  87. func Del(key string) (int, error) {
  88. redisClient := labelmsg.Get()
  89. defer redisClient.Close()
  90. reply, err := redisClient.Do("DEL", key)
  91. if err != nil {
  92. return 0, err
  93. }
  94. if reply == nil {
  95. return 0, err
  96. }
  97. s, _ := redis.Int(reply, nil)
  98. return s, nil
  99. }
  100. func TTL(key string) (int, error) {
  101. redisClient := labelmsg.Get()
  102. defer redisClient.Close()
  103. reply, err := redisClient.Do("TTL", key)
  104. if err != nil {
  105. return 0, err
  106. }
  107. n, _ := strconv.Atoi(fmt.Sprint(reply))
  108. return n, nil
  109. }