dao.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package block
  2. import (
  3. "context"
  4. "go-common/app/job/main/member/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. type notifyFunc func(context.Context, int64, string) error
  11. // Dao dao
  12. type Dao struct {
  13. conf *conf.Config
  14. mc *memcache.Pool
  15. db *xsql.DB
  16. httpClient *bm.Client
  17. notifyFunc notifyFunc
  18. }
  19. // New init mysql db
  20. func New(conf *conf.Config, mc *memcache.Pool, db *xsql.DB, client *bm.Client, notifyFunc notifyFunc) (dao *Dao) {
  21. dao = &Dao{
  22. conf: conf,
  23. mc: mc,
  24. db: db,
  25. httpClient: client,
  26. notifyFunc: notifyFunc,
  27. }
  28. return
  29. }
  30. // Close close the resource.
  31. func (d *Dao) Close() {
  32. d.mc.Close()
  33. d.db.Close()
  34. }
  35. // Ping dao ping
  36. func (d *Dao) Ping(c context.Context) (err error) {
  37. if err = d.db.Ping(c); err != nil {
  38. return
  39. }
  40. if err = d.pingMC(c); err != nil {
  41. return
  42. }
  43. return
  44. }
  45. // pingMc ping
  46. func (d *Dao) pingMC(c context.Context) (err error) {
  47. conn := d.mc.Get(c)
  48. defer conn.Close()
  49. return
  50. }
  51. // BeginTX is.
  52. func (d *Dao) BeginTX(c context.Context) (tx *xsql.Tx, err error) {
  53. if tx, err = d.db.Begin(c); err != nil {
  54. err = errors.WithStack(err)
  55. }
  56. return
  57. }