dao.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/spy/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/database/hbase.v2"
  9. "go-common/library/database/sql"
  10. bm "go-common/library/net/http/blademaster"
  11. )
  12. // Dao event dao def.
  13. type Dao struct {
  14. c *conf.Config
  15. db *sql.DB
  16. mc *memcache.Pool
  17. hbase *hbase.Client
  18. redis *redis.Pool
  19. httpClient *bm.Client
  20. expire int
  21. msgUUIDExpire int
  22. }
  23. // New create instance of dao and return.
  24. func New(c *conf.Config) (d *Dao) {
  25. d = &Dao{
  26. c: c,
  27. db: sql.NewMySQL(c.DB),
  28. mc: memcache.NewPool(c.Memcache),
  29. redis: redis.NewPool(c.Redis.Config),
  30. httpClient: bm.NewClient(c.HTTPClient),
  31. expire: int(time.Duration(c.Redis.Expire) / time.Second),
  32. msgUUIDExpire: int(time.Duration(c.Redis.MsgUUIDExpire) / time.Second),
  33. }
  34. if c.HBase != nil {
  35. d.hbase = hbase.NewClient(c.HBase.Config)
  36. }
  37. return
  38. }
  39. // Ping check db health.
  40. func (d *Dao) Ping(c context.Context) (err error) {
  41. if err = d.pingMC(c); err != nil {
  42. return
  43. }
  44. if err = d.PingRedis(c); err != nil {
  45. return
  46. }
  47. if d.hbase != nil {
  48. if err = d.hbase.Ping(c); err != nil {
  49. return
  50. }
  51. }
  52. return d.db.Ping(c)
  53. }
  54. // Close close all db connections.
  55. func (d *Dao) Close() {
  56. if d.db != nil {
  57. d.db.Close()
  58. }
  59. if d.redis != nil {
  60. d.redis.Close()
  61. }
  62. if d.mc != nil {
  63. d.mc.Close()
  64. }
  65. }