dao.go 869 B

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