dao.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/member/conf"
  6. "go-common/app/job/main/member/dao/block"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/cache/redis"
  9. "go-common/library/database/sql"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/queue/databus"
  12. )
  13. // Dao struct info of Dao.
  14. type Dao struct {
  15. c *conf.Config
  16. block *block.Dao
  17. db *sql.DB
  18. accCheckDB *sql.DB
  19. passLogDB *sql.DB
  20. accdb *sql.DB
  21. asodb *sql.DB
  22. client *bm.Client
  23. mc *memcache.Pool
  24. mcExpire int32
  25. redis *redis.Pool
  26. plogDatabus *databus.Databus
  27. accNotify *databus.Databus
  28. }
  29. // New new a Dao and return.
  30. func New(c *conf.Config) (d *Dao) {
  31. d = &Dao{
  32. c: c,
  33. db: sql.NewMySQL(c.Mysql),
  34. accCheckDB: sql.NewMySQL(c.AccCheckMysql),
  35. passLogDB: sql.NewMySQL(c.PasslogMysql),
  36. accdb: sql.NewMySQL(c.AccMysql),
  37. asodb: sql.NewMySQL(c.AsoMysql),
  38. client: bm.NewClient(c.HTTPClient),
  39. mc: memcache.NewPool(c.Memcache.Config),
  40. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  41. redis: redis.NewPool(c.Redis),
  42. plogDatabus: databus.New(c.PLogDatabus),
  43. accNotify: databus.New(c.AccountNotify),
  44. }
  45. d.block = block.New(c,
  46. memcache.NewPool(c.BlockMemcache),
  47. sql.NewMySQL(c.BlockDB),
  48. d.client,
  49. d.NotifyPurgeCache,
  50. )
  51. return
  52. }
  53. // Close close connections of mc, redis, db.
  54. func (d *Dao) Close() {
  55. if d.db != nil {
  56. d.db.Close()
  57. }
  58. if d.mc != nil {
  59. d.mc.Close()
  60. }
  61. if d.block != nil {
  62. d.block.Close()
  63. }
  64. }
  65. // Ping ping health of db.
  66. func (d *Dao) Ping(c context.Context) (err error) {
  67. return d.db.Ping(c)
  68. }
  69. // BlockImpl is.
  70. func (d *Dao) BlockImpl() *block.Dao {
  71. return d.block
  72. }