dao.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/spy/conf"
  7. account "go-common/app/service/main/account/rpc/client"
  8. spy "go-common/app/service/main/spy/rpc/client"
  9. "go-common/library/cache/memcache"
  10. "go-common/library/database/sql"
  11. )
  12. // Dao struct user of Dao.
  13. type Dao struct {
  14. c *conf.Config
  15. // db
  16. db *sql.DB
  17. getUserInfoStmt []*sql.Stmt
  18. eventStmt *sql.Stmt
  19. factorAllStmt *sql.Stmt
  20. allGroupStmt *sql.Stmt
  21. // cache
  22. mcUser *memcache.Pool
  23. mcUserExpire int32
  24. // rpc
  25. spyRPC *spy.Service
  26. accRPC *account.Service3
  27. }
  28. // New create a instance of Dao and return.
  29. func New(c *conf.Config) (d *Dao) {
  30. d = &Dao{
  31. // conf
  32. c: c,
  33. // db
  34. db: sql.NewMySQL(c.DB.Spy),
  35. // mc
  36. mcUser: memcache.NewPool(c.Memcache.User),
  37. mcUserExpire: int32(time.Duration(c.Memcache.UserExpire) / time.Second),
  38. // rpc
  39. spyRPC: spy.New(c.SpyRPC),
  40. accRPC: account.New3(c.AccountRPC),
  41. }
  42. if conf.Conf.Property.UserInfoShard <= 0 {
  43. panic("conf.Conf.Property.UserInfoShard <= 0")
  44. }
  45. if conf.Conf.Property.HistoryShard <= 0 {
  46. panic("conf.Conf.Property.HistoryShard <= 0")
  47. }
  48. d.getUserInfoStmt = make([]*sql.Stmt, conf.Conf.Property.UserInfoShard)
  49. for i := int64(0); i < conf.Conf.Property.UserInfoShard; i++ {
  50. d.getUserInfoStmt[i] = d.db.Prepared(fmt.Sprintf(_getUserInfoSQL, i))
  51. }
  52. d.eventStmt = d.db.Prepared(_eventSQL)
  53. d.factorAllStmt = d.db.Prepared(_factorAllSQL)
  54. d.allGroupStmt = d.db.Prepared(_allGroupSQL)
  55. return
  56. }
  57. // Ping check connection of db , mc.
  58. func (d *Dao) Ping(c context.Context) (err error) {
  59. return d.db.Ping(c)
  60. }
  61. // Close close connection of db , mc.
  62. func (d *Dao) Close() {
  63. if d.db != nil {
  64. d.db.Close()
  65. }
  66. }