dao.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/coin/conf"
  7. "go-common/library/cache/redis"
  8. "go-common/library/database/sql"
  9. "go-common/library/stat/prom"
  10. )
  11. // Dao define dao.
  12. type Dao struct {
  13. c *conf.Config
  14. coinDB *sql.DB
  15. hitSettlePeriodStmt *sql.Stmt
  16. getSettlePeriodStmt *sql.Stmt
  17. getTotalCoinsStmt []*sql.Stmt
  18. redis *redis.Pool
  19. loginExpire int32
  20. }
  21. // PromError .
  22. func PromError(name string) {
  23. prom.BusinessErrCount.Incr(name)
  24. }
  25. // New new and return service.
  26. func New(c *conf.Config) (d *Dao) {
  27. d = &Dao{
  28. c: c,
  29. coinDB: sql.NewMySQL(c.DB.Coin),
  30. getTotalCoinsStmt: make([]*sql.Stmt, SHARDING),
  31. redis: redis.NewPool(c.Redis),
  32. loginExpire: int32(time.Duration(c.CoinJob.LoginExpire) / time.Second),
  33. }
  34. for i := 0; i < SHARDING; i++ {
  35. d.getTotalCoinsStmt[i] = d.coinDB.Prepared(fmt.Sprintf(_getTotalCoins, i))
  36. }
  37. d.hitSettlePeriodStmt = d.coinDB.Prepared(_hitSettlePeriod)
  38. d.getSettlePeriodStmt = d.coinDB.Prepared(_getSettlePeriod)
  39. return
  40. }
  41. // Ping check service health.
  42. func (d *Dao) Ping(c context.Context) error {
  43. return d.coinDB.Ping(c)
  44. }
  45. // Close close sevice.
  46. func (d *Dao) Close() {
  47. d.coinDB.Close()
  48. }