redis.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/redis"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. _frozenDelayQueue = "fdq"
  10. _accLogin = "al:%d"
  11. )
  12. func accLoginKey(mid int64) string {
  13. return fmt.Sprintf(_accLogin, mid)
  14. }
  15. // RemQueue del a frozen user from redis sortset queue
  16. func (d *Dao) RemQueue(c context.Context, mid int64) (err error) {
  17. var (
  18. conn = d.redis.Get(c)
  19. )
  20. defer conn.Close()
  21. if _, err = conn.Do("ZREM", _frozenDelayQueue, mid); err != nil {
  22. err = errors.Wrap(err, "redis do zrem err")
  23. return
  24. }
  25. return
  26. }
  27. //DelRedisCache del redis cache.
  28. func (d *Dao) DelRedisCache(c context.Context, mid int64) (err error) {
  29. var (
  30. conn = d.redis.Get(c)
  31. key = accLoginKey(mid)
  32. )
  33. defer conn.Close()
  34. if _, err = conn.Do("DEL", key); err != nil {
  35. err = errors.WithStack(err)
  36. return
  37. }
  38. return
  39. }
  40. // LoginCount get user recent loggin count
  41. func (d *Dao) LoginCount(c context.Context, mid int64) (count int64, err error) {
  42. var conn = d.redis.Get(c)
  43. defer conn.Close()
  44. if count, err = redis.Int64(conn.Do("SCARD", accLoginKey(mid))); err != nil {
  45. err = errors.Wrap(err, "redis send zcard err(%+v)")
  46. }
  47. return
  48. }
  49. //FrozenTime get user frozen time
  50. func (d *Dao) FrozenTime(c context.Context, mid int64) (frozenTime int64, err error) {
  51. var conn = d.redis.Get(c)
  52. defer conn.Close()
  53. if frozenTime, err = redis.Int64(conn.Do("ZSCORE", _frozenDelayQueue, mid)); err != nil {
  54. if err == redis.ErrNil {
  55. err = nil
  56. return
  57. }
  58. err = errors.WithStack(err)
  59. return
  60. }
  61. return
  62. }