dao.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/service/main/passport-game/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. otherRegion *sql.DB
  18. mc *memcache.Pool
  19. mcExpire int32
  20. client *bm.Client
  21. myInfoURI string
  22. loginURI string
  23. getKeyURI string
  24. regV3URI string
  25. regV2URI string
  26. regURI string
  27. byTelURI string
  28. captchaURI string
  29. sendSmsURI string
  30. }
  31. const (
  32. _myInfoURI = "/api/myinfo"
  33. _loginURI = "/api/login"
  34. _getKeyURI = "/api/login/get_key"
  35. _regV3URI = "/api/reg/regV3"
  36. _regV2URI = "/api/reg/regV2"
  37. _regURI = "/api/reg/reg"
  38. _byTelURI = "/api/reg/byTelGame"
  39. _captchaURI = "/bilicaptcha/token"
  40. _sendSmsURI = "/api/sms/sendCaptcha"
  41. )
  42. // New dao
  43. func New(c *conf.Config) (d *Dao) {
  44. d = &Dao{
  45. c: c,
  46. cloudDB: sql.NewMySQL(c.DB.Cloud),
  47. otherRegion: sql.NewMySQL(c.DB.OtherRegion),
  48. mc: memcache.NewPool(c.Memcache.Config),
  49. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  50. client: bm.NewClient(c.HTTPClient),
  51. myInfoURI: c.AccountURI + _myInfoURI,
  52. loginURI: c.PassportURI + _loginURI,
  53. getKeyURI: c.PassportURI + _getKeyURI,
  54. regV3URI: c.PassportURI + _regV3URI,
  55. regV2URI: c.PassportURI + _regV2URI,
  56. regURI: c.PassportURI + _regURI,
  57. byTelURI: c.PassportURI + _byTelURI,
  58. captchaURI: c.PassportURI + _captchaURI,
  59. sendSmsURI: c.PassportURI + _sendSmsURI,
  60. }
  61. d.getMemberStmt = make([]*sql.Stmt, _memberShard)
  62. for i := 0; i < _memberShard; i++ {
  63. d.getMemberStmt[i] = d.cloudDB.Prepared(fmt.Sprintf(_getMemberInfoSQL, i))
  64. }
  65. return
  66. }
  67. // Ping verify server is ok.
  68. func (d *Dao) Ping(c context.Context) (err error) {
  69. if err = d.cloudDB.Ping(c); err != nil {
  70. log.Info("dao.cloudDB.Ping() error(%v)", err)
  71. }
  72. if err = d.otherRegion.Ping(c); err != nil {
  73. log.Info("dao.otherRegion.Ping() error(%v)", err)
  74. }
  75. return d.pingMC(c)
  76. }
  77. // Close close connections of mc, cloudDB.
  78. func (d *Dao) Close() (err error) {
  79. if d.cloudDB != nil {
  80. d.cloudDB.Close()
  81. }
  82. if d.otherRegion != nil {
  83. d.otherRegion.Close()
  84. }
  85. if d.mc != nil {
  86. d.mc.Close()
  87. }
  88. return
  89. }