dao.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/passport-game-cloud/conf"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. )
  12. // Dao dao
  13. type Dao struct {
  14. c *conf.Config
  15. getMemberStmt []*sql.Stmt
  16. cloudDB *sql.DB
  17. mc *memcache.Pool
  18. mcExpire int32
  19. gameClient *bm.Client
  20. delGameCacheURI string
  21. }
  22. // New new dao.
  23. func New(c *conf.Config) (d *Dao) {
  24. d = &Dao{
  25. c: c,
  26. cloudDB: sql.NewMySQL(c.DB.Cloud),
  27. mc: memcache.NewPool(c.Memcache.Config),
  28. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  29. gameClient: bm.NewClient(c.Game.Client),
  30. delGameCacheURI: c.Game.DelCacheURI,
  31. }
  32. d.getMemberStmt = make([]*sql.Stmt, _memberShard)
  33. for i := 0; i < _memberShard; i++ {
  34. d.getMemberStmt[i] = d.cloudDB.Prepared(fmt.Sprintf(_getMemberInfoSQL, i))
  35. }
  36. return
  37. }
  38. // Ping check dao ok.
  39. func (d *Dao) Ping(c context.Context) (err error) {
  40. if err = d.cloudDB.Ping(c); err != nil {
  41. log.Info("dao.cloudDB.Ping() error(%v)", err)
  42. }
  43. if err = d.pingMC(c); err != nil {
  44. log.Info("dao.pingMC() error(%v)", err)
  45. }
  46. return
  47. }
  48. // Close close connections of mc, cloudDB.
  49. func (d *Dao) Close() (err error) {
  50. if d.cloudDB != nil {
  51. d.cloudDB.Close()
  52. }
  53. if d.mc != nil {
  54. d.mc.Close()
  55. }
  56. return
  57. }