redis.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/member/model"
  6. "go-common/library/cache/redis"
  7. )
  8. const (
  9. _expShard = 10000
  10. _expAddedPrefix = "ea_%s_%d_%d"
  11. _expCoinPrefix = "ecoin_%d_%d"
  12. )
  13. const (
  14. _share = "shareClick"
  15. _view = "watch"
  16. _login = "login"
  17. )
  18. func expCoinKey(mid, day int64) string {
  19. return fmt.Sprintf(_expCoinPrefix, day, mid)
  20. }
  21. func expAddedKey(tp string, mid, day int64) string {
  22. return fmt.Sprintf(_expAddedPrefix, tp, day, mid/_expShard)
  23. }
  24. // StatCache get exp stat cache.
  25. func (d *Dao) StatCache(c context.Context, mid, day int64) (st *model.ExpStat, err error) {
  26. conn := d.redis.Get(c)
  27. defer conn.Close()
  28. conn.Send("GETBIT", expAddedKey(_login, mid, day), mid%_expShard)
  29. conn.Send("GETBIT", expAddedKey(_view, mid, day), mid%_expShard)
  30. conn.Send("GETBIT", expAddedKey(_share, mid, day), mid%_expShard)
  31. conn.Send("GET", expCoinKey(mid, day))
  32. err = conn.Flush()
  33. if err != nil {
  34. return
  35. }
  36. st = new(model.ExpStat)
  37. st.Login, err = redis.Bool(conn.Receive())
  38. if err != nil && err != redis.ErrNil {
  39. return
  40. }
  41. st.Watch, err = redis.Bool(conn.Receive())
  42. if err != nil && err != redis.ErrNil {
  43. return
  44. }
  45. st.Share, err = redis.Bool(conn.Receive())
  46. if err != nil && err != redis.ErrNil {
  47. return
  48. }
  49. st.Coin, err = redis.Int64(conn.Receive())
  50. if err != nil && err != redis.ErrNil {
  51. return
  52. }
  53. if err == redis.ErrNil {
  54. err = nil
  55. }
  56. return
  57. }