dao.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/openplatform/abtest/conf"
  6. "go-common/library/cache/redis"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. // Dao struct answer history of Dao
  11. type Dao struct {
  12. c *conf.Config
  13. // db
  14. db *sql.DB
  15. // redis
  16. redis *redis.Pool
  17. expire int
  18. verifyExpire int
  19. }
  20. // New new a Dao and return.
  21. func New(c *conf.Config) (d *Dao) {
  22. d = &Dao{
  23. c: c,
  24. db: sql.NewMySQL(c.DB.Ab),
  25. redis: redis.NewPool(c.Redis.Config),
  26. expire: int(time.Duration(c.Redis.Expire) / time.Second),
  27. verifyExpire: int(time.Duration(c.Redis.VerifyCdTimes) / time.Second),
  28. }
  29. return
  30. }
  31. // Close close connections.
  32. func (d *Dao) Close() {
  33. if d.db != nil {
  34. d.db.Close()
  35. }
  36. if d.redis != nil {
  37. d.redis.Close()
  38. }
  39. }
  40. // Ping ping health.
  41. func (d *Dao) Ping(c context.Context) (err error) {
  42. if err = d.db.Ping(c); err != nil {
  43. log.Error("PingDb error(%v)", err)
  44. return
  45. }
  46. if err = d.PingRedis(c); err != nil {
  47. log.Error("PingRedis error(%v)", err)
  48. return
  49. }
  50. return
  51. }