dao.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/job/main/block/conf"
  5. "go-common/library/cache/memcache"
  6. xsql "go-common/library/database/sql"
  7. bm "go-common/library/net/http/blademaster"
  8. "github.com/pkg/errors"
  9. )
  10. // Dao dao
  11. type Dao struct {
  12. mc *memcache.Pool
  13. db *xsql.DB
  14. httpClient *bm.Client
  15. }
  16. // New init mysql db
  17. func New() (dao *Dao) {
  18. dao = &Dao{
  19. mc: memcache.NewPool(conf.Conf.Memcache),
  20. db: xsql.NewMySQL(conf.Conf.DB),
  21. httpClient: bm.NewClient(conf.Conf.HTTPClient),
  22. }
  23. return
  24. }
  25. // Close close the resource.
  26. func (d *Dao) Close() {
  27. d.mc.Close()
  28. d.db.Close()
  29. }
  30. // Ping dao ping
  31. func (d *Dao) Ping(c context.Context) (err error) {
  32. if err = d.db.Ping(c); err != nil {
  33. return
  34. }
  35. if err = d.pingMC(c); err != nil {
  36. return
  37. }
  38. return
  39. }
  40. // pingMc ping
  41. func (d *Dao) pingMC(c context.Context) (err error) {
  42. conn := d.mc.Get(c)
  43. defer conn.Close()
  44. return
  45. }
  46. func (d *Dao) BeginTX(c context.Context) (tx *xsql.Tx, err error) {
  47. if tx, err = d.db.Begin(c); err != nil {
  48. err = errors.WithStack(err)
  49. }
  50. return
  51. }