dao.go 1.3 KB

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