dao.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/ep/saga/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/database/orm"
  9. bm "go-common/library/net/http/blademaster"
  10. "github.com/jinzhu/gorm"
  11. )
  12. // Dao def
  13. type Dao struct {
  14. // cache
  15. httpClient *bm.Client
  16. db *gorm.DB
  17. mc *memcache.Pool
  18. redis *redis.Pool
  19. mcRecordExpire int32
  20. }
  21. // New create instance of Dao
  22. func New() (d *Dao) {
  23. d = &Dao{
  24. mc: memcache.NewPool(conf.Conf.Memcache.MC),
  25. httpClient: bm.NewClient(conf.Conf.HTTPClient),
  26. db: orm.NewMySQL(conf.Conf.ORM),
  27. redis: redis.NewPool(conf.Conf.Redis),
  28. mcRecordExpire: int32(time.Duration(conf.Conf.Memcache.MCRecordExpire) / time.Second),
  29. }
  30. return
  31. }
  32. // Ping dao.
  33. func (d *Dao) Ping(c context.Context) (err error) {
  34. if err = d.pingRedis(c); err != nil {
  35. return
  36. }
  37. if err = d.pingMC(c); err != nil {
  38. return
  39. }
  40. return d.db.DB().Ping()
  41. }
  42. // Close dao.
  43. func (d *Dao) Close() {
  44. if d.mc != nil {
  45. d.mc.Close()
  46. }
  47. if d.db != nil {
  48. d.db.Close()
  49. }
  50. }