dao.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/vip/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/database/orm"
  8. xsql "go-common/library/database/sql"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/stat/prom"
  11. "github.com/jinzhu/gorm"
  12. )
  13. // Dao dao conf
  14. type Dao struct {
  15. c *conf.Config
  16. db *xsql.DB
  17. vip *gorm.DB
  18. client *bm.Client
  19. mc *memcache.Pool
  20. mcExpire int32
  21. errProm *prom.Prom
  22. }
  23. // New init mysql db
  24. func New(c *conf.Config) (d *Dao) {
  25. d = &Dao{
  26. c: c,
  27. db: xsql.NewMySQL(c.MySQL),
  28. vip: orm.NewMySQL(c.ORM.Vip),
  29. client: bm.NewClient(c.HTTPClient),
  30. mc: memcache.NewPool(c.Memcache.Config),
  31. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  32. errProm: prom.BusinessErrCount,
  33. }
  34. d.initORM()
  35. return d
  36. }
  37. // Close close the resource.
  38. func (d *Dao) Close() (err error) {
  39. return d.db.Close()
  40. }
  41. // Ping dao ping
  42. func (d *Dao) Ping(c context.Context) error {
  43. return d.db.Ping(c)
  44. }
  45. // BeginTran start tx .
  46. func (d *Dao) BeginTran(c context.Context) (tx *xsql.Tx, err error) {
  47. return d.db.Begin(c)
  48. }
  49. // BeginGormTran start gorm tx .
  50. func (d *Dao) BeginGormTran(c context.Context) (tx *gorm.DB) {
  51. return d.vip.Begin()
  52. }
  53. func (d *Dao) initORM() {
  54. d.vip.LogMode(true)
  55. }