redis.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/library/cache/redis"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _frozenDelayQueue = "fdq"
  11. _accLogin = "al:%d"
  12. )
  13. func accLoginKey(mid int64) string {
  14. return fmt.Sprintf(_accLogin, mid)
  15. }
  16. // Enqueue put frozen user to redis sortset queue
  17. func (d *Dao) Enqueue(c context.Context, mid, score int64) (err error) {
  18. var (
  19. conn = d.redis.Get(c)
  20. )
  21. defer conn.Close()
  22. if err = conn.Send("ZADD", _frozenDelayQueue, score, mid); err != nil {
  23. err = errors.Wrap(err, "redis send zadd err(%+v)")
  24. return
  25. }
  26. if err = conn.Send("EXPIRE", _frozenDelayQueue, d.redisExpire); err != nil {
  27. err = errors.Wrap(err, "redis send expire err(%+v)")
  28. return
  29. }
  30. if err = conn.Flush(); err != nil {
  31. return
  32. }
  33. for i := 0; i < 2; i++ {
  34. if _, err = conn.Receive(); err != nil {
  35. return
  36. }
  37. }
  38. return
  39. }
  40. // Dequeue get a frozen user from redis sortset queue
  41. func (d *Dao) Dequeue(c context.Context) (mid []int64, err error) {
  42. var (
  43. conn = d.redis.Get(c)
  44. from = time.Now().Add(-1 * time.Minute).Unix()
  45. to = time.Now().Unix()
  46. )
  47. defer conn.Close()
  48. if mid, err = redis.Int64s(conn.Do("ZRANGEBYSCORE", _frozenDelayQueue, from, to)); err != nil {
  49. err = errors.Wrap(err, "redis do zrevrangebyscore err")
  50. return
  51. }
  52. return
  53. }
  54. // RemQueue del a frozen user from redis sortset queue
  55. func (d *Dao) RemQueue(c context.Context, mid int64) (err error) {
  56. var (
  57. conn = d.redis.Get(c)
  58. )
  59. defer conn.Close()
  60. if _, err = conn.Do("ZREM", _frozenDelayQueue, mid); err != nil {
  61. err = errors.Wrap(err, "redis do zrem err")
  62. return
  63. }
  64. return
  65. }
  66. // AddLogginIP save user loggin info to sortset
  67. func (d *Dao) AddLogginIP(c context.Context, mid int64, ip uint32) (err error) {
  68. var (
  69. conn = d.redis.Get(c)
  70. key = accLoginKey(mid)
  71. )
  72. defer conn.Close()
  73. if err = conn.Send("SADD", key, ip); err != nil {
  74. err = errors.Wrap(err, "redis do zadd err")
  75. return
  76. }
  77. if err = conn.Send("EXPIRE", key, d.frozenExpire); err != nil {
  78. err = errors.Wrap(err, "redis send expire err(%+v)")
  79. return
  80. }
  81. if err = conn.Flush(); err != nil {
  82. return
  83. }
  84. for i := 0; i < 2; i++ {
  85. if _, err = conn.Receive(); err != nil {
  86. return
  87. }
  88. }
  89. return
  90. }
  91. //DelCache del cache.
  92. func (d *Dao) DelCache(c context.Context, mid int64) (err error) {
  93. var (
  94. conn = d.redis.Get(c)
  95. key = accLoginKey(mid)
  96. )
  97. defer conn.Close()
  98. if _, err = conn.Do("DEL", key); err != nil {
  99. err = errors.WithStack(err)
  100. return
  101. }
  102. return
  103. }
  104. // LoginCount get user recent loggin count
  105. func (d *Dao) LoginCount(c context.Context, mid int64) (count int64, err error) {
  106. var conn = d.redis.Get(c)
  107. defer conn.Close()
  108. if count, err = redis.Int64(conn.Do("SCARD", accLoginKey(mid))); err != nil {
  109. err = errors.Wrap(err, "redis send zcard err(%+v)")
  110. }
  111. return
  112. }