dao.go 833 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/passport-auth/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/database/sql"
  8. )
  9. // Dao dao struct
  10. type Dao struct {
  11. db *sql.DB
  12. mc *memcache.Pool
  13. mcExpire int32
  14. c *conf.Config
  15. }
  16. // New create new dao
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. c: c,
  20. db: sql.NewMySQL(c.Mysql),
  21. mc: memcache.NewPool(c.Memcache.Config),
  22. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  23. }
  24. return
  25. }
  26. // Ping check db and mc health.
  27. func (d *Dao) Ping(c context.Context) (err error) {
  28. if err = d.db.Ping(c); err != nil {
  29. return
  30. }
  31. return d.pingMC(c)
  32. }
  33. // Close close connections of mc, redis, db.
  34. func (d *Dao) Close() {
  35. if d.mc != nil {
  36. d.mc.Close()
  37. }
  38. if d.db != nil {
  39. d.db.Close()
  40. }
  41. }