dao.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/thumbup/conf"
  6. "go-common/library/cache/memcache"
  7. xredis "go-common/library/cache/redis"
  8. "go-common/library/database/tidb"
  9. "go-common/library/log"
  10. "go-common/library/queue/databus"
  11. )
  12. // Dao .
  13. type Dao struct {
  14. // config
  15. c *conf.Config
  16. // tidb
  17. tidb *tidb.DB
  18. itemTidb *tidb.DB
  19. // memcache
  20. mc *memcache.Pool
  21. mcStatsExpire int32
  22. // redis
  23. redis *xredis.Pool
  24. redisStatsExpire int64
  25. redisUserLikesExpire int64
  26. redisItemLikesExpire int64
  27. // databus
  28. statDbus *databus.Databus
  29. // stmt
  30. businessesStmt *tidb.Stmts
  31. itemLikesStmt *tidb.Stmts
  32. userLikesStmt *tidb.Stmts
  33. likeStateStmt *tidb.Stmts
  34. statStmt *tidb.Stmts
  35. updateLikeStateStmt *tidb.Stmts
  36. }
  37. // New .
  38. func New(c *conf.Config) (d *Dao) {
  39. d = &Dao{
  40. c: c,
  41. // tidb
  42. tidb: tidb.NewTiDB(c.Tidb),
  43. itemTidb: tidb.NewTiDB(c.ItemTidb),
  44. // memcache
  45. mc: memcache.NewPool(c.Memcache.Config),
  46. mcStatsExpire: int32(time.Duration(c.Memcache.StatsExpire) / time.Second),
  47. // redis
  48. redis: xredis.NewPool(c.Redis.Config),
  49. redisStatsExpire: int64(time.Duration(c.Redis.StatsExpire) / time.Second),
  50. redisUserLikesExpire: int64(time.Duration(c.Redis.UserLikesExpire) / time.Second),
  51. redisItemLikesExpire: int64(time.Duration(c.Redis.ItemLikesExpire) / time.Second),
  52. // databus
  53. statDbus: databus.New(c.Databus.Stat),
  54. }
  55. d.statStmt = d.tidb.Prepared(_statSQL)
  56. d.itemLikesStmt = d.itemTidb.Prepared(_itemLikesSQL)
  57. d.businessesStmt = d.tidb.Prepared(_businessesSQL)
  58. d.userLikesStmt = d.tidb.Prepared(_userLikeListSQL)
  59. d.likeStateStmt = d.tidb.Prepared(_likeStateSQL)
  60. d.updateLikeStateStmt = d.tidb.Prepared(_updateLikeStateSQL)
  61. return
  62. }
  63. // Close .
  64. func (d *Dao) Close() {
  65. d.mc.Close()
  66. d.tidb.Close()
  67. d.redis.Close()
  68. }
  69. // Ping .
  70. func (d *Dao) Ping(c context.Context) (err error) {
  71. if err = d.pingMC(c); err != nil {
  72. log.Error("d.pingMC error(%v)", err)
  73. return
  74. }
  75. if err = d.pingRedis(c); err != nil {
  76. log.Error("d.pingRedis error(%v)", err)
  77. }
  78. return
  79. }
  80. // pingMC ping memcache
  81. func (d *Dao) pingMC(c context.Context) (err error) {
  82. conn := d.mc.Get(c)
  83. defer conn.Close()
  84. item := memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 100}
  85. err = conn.Set(&item)
  86. return
  87. }
  88. // pingRedis ping redis.
  89. func (d *Dao) pingRedis(c context.Context) (err error) {
  90. conn := d.redis.Get(c)
  91. defer conn.Close()
  92. _, err = conn.Do("SET", "PING", "PONG")
  93. return
  94. }