dao.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/job/main/ugcpay/conf"
  5. ugcpay_rank "go-common/app/service/main/ugcpay-rank/api/v1"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. xsql "go-common/library/database/sql"
  9. )
  10. // Dao dao
  11. type Dao struct {
  12. c *conf.Config
  13. mc *memcache.Pool
  14. mcRank *memcache.Pool
  15. redis *redis.Pool
  16. db *xsql.DB
  17. dbrank *xsql.DB
  18. dbrankold *xsql.DB
  19. ugcPayRankAPI ugcpay_rank.UGCPayRankClient
  20. }
  21. // New init mysql db
  22. func New(c *conf.Config) (dao *Dao) {
  23. dao = &Dao{
  24. c: c,
  25. mc: memcache.NewPool(c.Memcache),
  26. mcRank: memcache.NewPool(c.MemcacheRank),
  27. redis: redis.NewPool(c.Redis),
  28. db: xsql.NewMySQL(c.MySQL),
  29. dbrank: xsql.NewMySQL(c.MySQLRank),
  30. dbrankold: xsql.NewMySQL(c.MySQLRankOld),
  31. }
  32. var err error
  33. if dao.ugcPayRankAPI, err = ugcpay_rank.NewClient(c.GRPCUGCPayRank); err != nil {
  34. panic(err)
  35. }
  36. return
  37. }
  38. // Close close the resource.
  39. func (d *Dao) Close() {
  40. d.mc.Close()
  41. d.redis.Close()
  42. d.db.Close()
  43. }
  44. // Ping dao ping
  45. func (d *Dao) Ping(c context.Context) error {
  46. return nil
  47. }
  48. // BeginTran begin transaction.
  49. func (d *Dao) BeginTran(c context.Context) (*xsql.Tx, error) {
  50. return d.db.Begin(c)
  51. }
  52. // BeginTranRank .
  53. func (d *Dao) BeginTranRank(c context.Context) (*xsql.Tx, error) {
  54. return d.dbrank.Begin(c)
  55. }