dao.go 952 B

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