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

3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }