dao.go 875 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/service/main/open/conf"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/database/orm"
  7. "go-common/library/log"
  8. "github.com/jinzhu/gorm"
  9. )
  10. // Dao .
  11. type Dao struct {
  12. // db
  13. DB *gorm.DB
  14. //memcache
  15. mc *memcache.Pool
  16. }
  17. // New new a instance.
  18. func New(c *conf.Config) (d *Dao) {
  19. d = &Dao{
  20. // db
  21. DB: orm.NewMySQL(c.ORM),
  22. //memcache
  23. mc: memcache.NewPool(c.Memcache),
  24. }
  25. d.initORM()
  26. return
  27. }
  28. func (d *Dao) initORM() {
  29. d.DB.LogMode(true)
  30. }
  31. // Ping check connection of db , mc .
  32. func (d *Dao) Ping(c context.Context) (err error) {
  33. if d.DB != nil {
  34. if err = d.DB.DB().PingContext(c); err != nil {
  35. log.Error("d.PingContext error (%v)", err)
  36. }
  37. }
  38. if err = d.pingMC(c); err != nil {
  39. return
  40. }
  41. return
  42. }
  43. // Close close connection of db.
  44. func (d *Dao) Close() {
  45. if d.DB != nil {
  46. d.DB.Close()
  47. }
  48. }