dao.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/figure-timer/conf"
  6. "go-common/app/job/main/figure-timer/model"
  7. "go-common/library/cache/redis"
  8. "go-common/library/database/hbase.v2"
  9. "go-common/library/database/sql"
  10. )
  11. // Int is dao interface
  12. type Int interface {
  13. HBase
  14. Mysql
  15. Redis
  16. Close()
  17. Ping(c context.Context) error
  18. }
  19. // HBase dao hbase interface
  20. type HBase interface {
  21. UserInfo(c context.Context, mid int64, weekVer int64) (userInfo *model.UserInfo, err error)
  22. PutCalcRecord(c context.Context, record *model.FigureRecord, weekTS int64) (err error)
  23. CalcRecords(c context.Context, mid int64, weekTSFrom, weekTSTo int64) (figureRecords []*model.FigureRecord, err error)
  24. ActionCounter(c context.Context, mid int64, ts int64) (counter *model.ActionCounter, err error)
  25. }
  26. // Mysql dao mysql interface
  27. type Mysql interface {
  28. Figure(c context.Context, mid int64) (figure *model.Figure, err error)
  29. Figures(c context.Context, fromMid int64, limit int) (figures []*model.Figure, end bool, err error)
  30. UpsertFigure(c context.Context, figure *model.Figure) (id int64, err error)
  31. InsertRankHistory(c context.Context, rank *model.Rank) (id int64, err error)
  32. UpsertRank(c context.Context, rank *model.Rank) (id int64, err error)
  33. }
  34. // Redis dao redis interface
  35. type Redis interface {
  36. FigureCache(c context.Context, mid int64) (figure *model.Figure, err error)
  37. SetFigureCache(c context.Context, figure *model.Figure) (err error)
  38. PendingMidsCache(c context.Context, version int64, shard int64) (mids []int64, err error)
  39. RemoveCache(c context.Context, mid int64) (err error)
  40. }
  41. // Dao struct info of Dao.
  42. type Dao struct {
  43. c *conf.Config
  44. mysql *sql.DB
  45. hbase *hbase.Client
  46. redis *redis.Pool
  47. redisExpire int32
  48. }
  49. // New new a Dao and return.
  50. func New(c *conf.Config) (d *Dao) {
  51. d = &Dao{
  52. c: c,
  53. mysql: sql.NewMySQL(c.Mysql),
  54. hbase: hbase.NewClient(c.Hbase.Config),
  55. redis: redis.NewPool(c.Redis.Config),
  56. redisExpire: int32(time.Duration(c.Redis.Expire) / time.Second),
  57. }
  58. return
  59. }
  60. // Close close connections of mc, redis, db.
  61. func (d *Dao) Close() {
  62. if d.mysql != nil {
  63. d.mysql.Close()
  64. }
  65. if d.redis != nil {
  66. d.redis.Close()
  67. }
  68. if d.hbase != nil {
  69. d.hbase.Close()
  70. }
  71. }
  72. // Ping ping health of db.
  73. func (d *Dao) Ping(c context.Context) (err error) {
  74. if err = d.mysql.Ping(c); err != nil {
  75. return
  76. }
  77. // if err = d.hbase.Ping(c); err != nil {
  78. // return
  79. // }
  80. if err = d.PingRedis(c); err != nil {
  81. return
  82. }
  83. return
  84. }