dao.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/passport-user/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. // Dao dao
  11. type Dao struct {
  12. c *conf.Config
  13. originDB *sql.DB
  14. userDB *sql.DB
  15. encryptDB *sql.DB
  16. mc *memcache.Pool
  17. mcExpire int32
  18. }
  19. // New new dao.
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. c: c,
  23. originDB: sql.NewMySQL(c.DB.OriginDB),
  24. userDB: sql.NewMySQL(c.DB.UserDB),
  25. encryptDB: sql.NewMySQL(c.DB.EncryptDB),
  26. mc: memcache.NewPool(c.Memcache.Config),
  27. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  28. }
  29. return
  30. }
  31. // Ping check dao ok.
  32. func (d *Dao) Ping(c context.Context) (err error) {
  33. if err = d.originDB.Ping(c); err != nil {
  34. log.Info("dao.originDB.Ping() error(%v)", err)
  35. }
  36. if err = d.userDB.Ping(c); err != nil {
  37. log.Info("dao.userDB.Ping() error(%v)", err)
  38. }
  39. if err = d.encryptDB.Ping(c); err != nil {
  40. log.Info("dao.encryptDB.Ping() error(%v)", err)
  41. }
  42. if err = d.pingMC(c); err != nil {
  43. log.Info("d.pingMC() error(%v)", err)
  44. }
  45. return
  46. }
  47. // Close close connections of mc, cloudDB.
  48. func (d *Dao) Close() (err error) {
  49. if d.originDB != nil {
  50. d.originDB.Close()
  51. }
  52. if d.userDB != nil {
  53. d.userDB.Close()
  54. }
  55. if d.encryptDB != nil {
  56. d.encryptDB.Close()
  57. }
  58. if d.mc != nil {
  59. d.mc.Close()
  60. }
  61. return
  62. }
  63. // BeginTran begin transcation.
  64. func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
  65. return d.userDB.Begin(c)
  66. }