dao.go 780 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/figure/conf"
  6. "go-common/library/cache/redis"
  7. "go-common/library/database/sql"
  8. )
  9. // Dao figure DAO
  10. type Dao struct {
  11. c *conf.Config
  12. db *sql.DB
  13. redis *redis.Pool
  14. redisExpire int32
  15. }
  16. // New new a figure DAO
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. c: c,
  20. db: sql.NewMySQL(c.Mysql),
  21. redis: redis.NewPool(c.Redis.Config),
  22. redisExpire: int32(time.Duration(c.Redis.Expire) / time.Second),
  23. }
  24. return
  25. }
  26. // Ping check service health
  27. func (d *Dao) Ping(c context.Context) (err error) {
  28. if err = d.PingRedis(c); err != nil {
  29. return
  30. }
  31. return d.db.Ping(c)
  32. }
  33. // Close close all dao.
  34. func (d *Dao) Close() {
  35. if d.db != nil {
  36. d.db.Close()
  37. }
  38. }