dao.go 972 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package dao
  2. import (
  3. "go-common/app/admin/main/apm/conf"
  4. "go-common/library/cache/redis"
  5. "go-common/library/database/orm"
  6. bm "go-common/library/net/http/blademaster"
  7. "github.com/jinzhu/gorm"
  8. )
  9. // Dao dao.
  10. type Dao struct {
  11. c *conf.Config
  12. DB *gorm.DB
  13. DBDatabus *gorm.DB
  14. DBCanal *gorm.DB
  15. // client
  16. client *bm.Client
  17. Redis *redis.Pool
  18. }
  19. // New new a dao.
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. c: c,
  23. DB: orm.NewMySQL(c.ORM),
  24. DBDatabus: orm.NewMySQL(c.ORMDatabus),
  25. DBCanal: orm.NewMySQL(c.ORMCanal),
  26. client: bm.NewClient(c.HTTPClient),
  27. Redis: redis.NewPool(c.Redis.Config),
  28. }
  29. d.initORM()
  30. return
  31. }
  32. func (d *Dao) initORM() {
  33. d.DB.LogMode(true)
  34. d.DBDatabus.LogMode(true)
  35. d.DBCanal.LogMode(true)
  36. }
  37. // Close close connection of db , mc.
  38. func (d *Dao) Close() {
  39. if d.DB != nil {
  40. d.DB.Close()
  41. }
  42. if d.DBDatabus != nil {
  43. d.DBDatabus.Close()
  44. }
  45. if d.DBCanal != nil {
  46. d.DBCanal.Close()
  47. }
  48. }