dao.go 971 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/job/main/point/conf"
  5. "go-common/library/cache/memcache"
  6. xsql "go-common/library/database/sql"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. // Dao dao
  10. type Dao struct {
  11. c *conf.Config
  12. mc *memcache.Pool
  13. db *xsql.DB
  14. client *bm.Client
  15. }
  16. // New init mysql db
  17. func New(c *conf.Config) (dao *Dao) {
  18. dao = &Dao{
  19. c: c,
  20. mc: memcache.NewPool(c.Memcache),
  21. db: xsql.NewMySQL(c.MySQL),
  22. client: bm.NewClient(c.HTTPClient),
  23. }
  24. return
  25. }
  26. // Close close the resource.
  27. func (dao *Dao) Close() {
  28. dao.mc.Close()
  29. dao.db.Close()
  30. }
  31. // Ping dao ping
  32. func (dao *Dao) Ping(c context.Context) (err error) {
  33. if err = dao.db.Ping(c); err != nil {
  34. return
  35. }
  36. err = dao.pingMC(c)
  37. return
  38. }
  39. // pingMc ping
  40. func (dao *Dao) pingMC(c context.Context) (err error) {
  41. conn := dao.mc.Get(c)
  42. defer conn.Close()
  43. item := memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 60}
  44. return conn.Set(&item)
  45. }