dao.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/job/bbq/recall/internal/conf"
  5. recall "go-common/app/service/bbq/recsys-recall/api/grpc/v1"
  6. "go-common/library/cache/redis"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/net/rpc/warden"
  9. )
  10. // Dao dao
  11. type Dao struct {
  12. c *conf.Config
  13. redis *redis.Pool
  14. bfredis *redis.Pool
  15. db *xsql.DB
  16. dbOffline *xsql.DB
  17. dbCms *xsql.DB
  18. recallClient recall.RecsysRecallClient
  19. }
  20. // New init mysql db
  21. func New(c *conf.Config) (dao *Dao) {
  22. dao = &Dao{
  23. c: c,
  24. redis: redis.NewPool(c.Redis),
  25. bfredis: redis.NewPool(c.BfRedis),
  26. db: xsql.NewMySQL(c.MySQL),
  27. dbOffline: xsql.NewMySQL(c.OfflineMySQL),
  28. dbCms: xsql.NewMySQL(c.CmsMySQL),
  29. recallClient: newRecallClient(c.GRPCClient["recall"]),
  30. }
  31. return
  32. }
  33. func newRecallClient(cfg *conf.GRPCConfig) recall.RecsysRecallClient {
  34. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  35. if err != nil {
  36. panic(err)
  37. }
  38. return recall.NewRecsysRecallClient(cc)
  39. }
  40. // Close close the resource.
  41. func (d *Dao) Close() {
  42. d.redis.Close()
  43. d.db.Close()
  44. }
  45. // Ping dao ping
  46. func (d *Dao) Ping(ctx context.Context) error {
  47. // TODO: add mc,redis... if you use
  48. return d.db.Ping(ctx)
  49. }