dao.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/usersuit/conf"
  6. "go-common/library/cache/redis"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. const (
  12. _beFormal = "/api/member/beFormal"
  13. )
  14. // Dao struct answer history of Dao
  15. type Dao struct {
  16. db *sql.DB
  17. redis *redis.Pool
  18. //http
  19. httpClient *bm.Client
  20. c *conf.Config
  21. beFormalURI string
  22. inviteExpire int32
  23. }
  24. // New new a Dao and return.
  25. func New(c *conf.Config) (d *Dao) {
  26. d = &Dao{
  27. c: c,
  28. db: sql.NewMySQL(c.MySQL),
  29. redis: redis.NewPool(c.Redis.Config),
  30. inviteExpire: int32(time.Duration(c.Redis.InviteExpire) / time.Second),
  31. httpClient: bm.NewClient(c.HTTPClient),
  32. beFormalURI: c.AccountIntranetURI + _beFormal,
  33. }
  34. return
  35. }
  36. // Close close connections of mc, redis, db.
  37. func (d *Dao) Close() {
  38. d.db.Close()
  39. d.redis.Close()
  40. }
  41. // Ping ping health.
  42. func (d *Dao) Ping(c context.Context) (err error) {
  43. if err = d.db.Ping(c); err != nil {
  44. log.Error("dao.db.Ping() error(%v)", err)
  45. return
  46. }
  47. if err = d.pingRedis(c); err != nil {
  48. log.Error("dao.pingRedis() error(%v)", err)
  49. }
  50. return
  51. }