dao.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/reply-feed/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. xsql "go-common/library/database/sql"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. // Dao dao
  12. type Dao struct {
  13. c *conf.Config
  14. mc *memcache.Pool
  15. mcExpire int32
  16. redis *redis.Pool
  17. redisReplySetExpire int
  18. redisReplyZSetExpire int
  19. redisRefreshExpire int
  20. db *xsql.DB
  21. dbSlave *xsql.DB
  22. httpCli *bm.Client
  23. }
  24. // New init mysql db
  25. func New(c *conf.Config) (dao *Dao) {
  26. dao = &Dao{
  27. c: c,
  28. mc: memcache.NewPool(c.Memcache),
  29. mcExpire: int32(time.Duration(c.MemcacheExpire.McStatExpire) / time.Second),
  30. redis: redis.NewPool(c.Redis),
  31. redisReplySetExpire: int(time.Duration(c.RedisExpire.RedisReplySetExpire) / time.Second),
  32. redisReplyZSetExpire: int(time.Duration(c.RedisExpire.RedisReplyZSetExpire) / time.Second),
  33. redisRefreshExpire: int(time.Duration(c.RedisExpire.RedisRefreshExpire) / time.Second),
  34. db: xsql.NewMySQL(c.MySQL.DB),
  35. dbSlave: xsql.NewMySQL(c.MySQL.DBSlave),
  36. httpCli: bm.NewClient(c.HTTPClient),
  37. }
  38. return
  39. }
  40. // Close close the resource.
  41. func (d *Dao) Close() {
  42. d.mc.Close()
  43. d.redis.Close()
  44. d.dbSlave.Close()
  45. d.db.Close()
  46. }
  47. // Ping dao ping
  48. func (d *Dao) Ping(c context.Context) error {
  49. if err := d.PingRedis(c); err != nil {
  50. return err
  51. }
  52. if err := d.PingMc(c); err != nil {
  53. return err
  54. }
  55. return d.db.Ping(c)
  56. }