dao.go 959 B

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