dao.go 1.0 KB

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