dao.go 961 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package pendant
  2. import (
  3. "context"
  4. "go-common/app/job/main/usersuit/conf"
  5. "go-common/library/cache/redis"
  6. "go-common/library/database/sql"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. // Dao struct info of Dao.
  10. type Dao struct {
  11. db *sql.DB
  12. c *conf.Config
  13. client *bm.Client
  14. // redis
  15. redis *redis.Pool
  16. notifyURL string
  17. }
  18. // New new a Dao and return.
  19. func New(c *conf.Config) (d *Dao) {
  20. d = &Dao{
  21. c: c,
  22. db: sql.NewMySQL(c.Mysql),
  23. client: bm.NewClient(c.HTTPClient),
  24. // redis
  25. redis: redis.NewPool(c.PendantRedis.Config),
  26. notifyURL: c.NotifyURL,
  27. }
  28. return
  29. }
  30. // Ping ping health.
  31. func (d *Dao) Ping(c context.Context) (err error) {
  32. return d.pingRedis(c)
  33. }
  34. // Close close connections of mc, redis, db.
  35. func (d *Dao) Close() {
  36. if d.db != nil {
  37. d.db.Close()
  38. }
  39. }
  40. func (d *Dao) pingRedis(c context.Context) (err error) {
  41. conn := d.redis.Get(c)
  42. _, err = conn.Do("SET", "PING", "PONG")
  43. conn.Close()
  44. return
  45. }