dao.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/up-rating/conf"
  6. "go-common/library/cache/redis"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. // Dao def dao struct
  11. type Dao struct {
  12. c *conf.Config
  13. db *sql.DB
  14. rddb *sql.DB
  15. redis *redis.Pool
  16. upRatingExpire int64
  17. }
  18. // New fn
  19. func New(c *conf.Config) (d *Dao) {
  20. d = &Dao{
  21. c: c,
  22. db: sql.NewMySQL(c.DB.Main),
  23. rddb: sql.NewMySQL(c.DB.Slave),
  24. redis: redis.NewPool(c.Redis.Config),
  25. upRatingExpire: int64(time.Duration(c.Redis.UpRatingExpire) / time.Second),
  26. }
  27. return d
  28. }
  29. // Ping ping db
  30. func (d *Dao) Ping(c context.Context) (err error) {
  31. if err = d.db.Ping(c); err != nil {
  32. log.Error("d.db.Ping error(%v)", err)
  33. return
  34. }
  35. return
  36. }
  37. // Close close db conn
  38. func (d *Dao) Close() {
  39. if d.db != nil {
  40. d.db.Close()
  41. }
  42. if d.redis != nil {
  43. d.redis.Close()
  44. }
  45. }
  46. // BeginTran begin transcation
  47. func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
  48. return d.db.Begin(c)
  49. }