dao.go 888 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/card/conf"
  6. "go-common/library/cache"
  7. "go-common/library/cache/memcache"
  8. xsql "go-common/library/database/sql"
  9. )
  10. // Dao dao
  11. type Dao struct {
  12. c *conf.Config
  13. // memcache
  14. mc *memcache.Pool
  15. mcExpire int32
  16. // db
  17. db *xsql.DB
  18. // cache async save
  19. cache *cache.Cache
  20. }
  21. // New init mysql db
  22. func New(c *conf.Config) (dao *Dao) {
  23. dao = &Dao{
  24. c: c,
  25. // card memcache
  26. mc: memcache.NewPool(c.Memcache.Config),
  27. mcExpire: int32(time.Duration(c.Memcache.CardExpire) / time.Second),
  28. db: xsql.NewMySQL(c.MySQL),
  29. // cache chan
  30. cache: cache.New(1, 1024),
  31. }
  32. return
  33. }
  34. // Close close the resource.
  35. func (d *Dao) Close() {
  36. d.mc.Close()
  37. d.db.Close()
  38. }
  39. // Ping dao ping
  40. func (d *Dao) Ping(c context.Context) (err error) {
  41. if err = d.pingMC(c); err != nil {
  42. return
  43. }
  44. return d.db.Ping(c)
  45. }