dao.go 814 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package dao
  2. import (
  3. "context"
  4. "go-common/library/cache/memcache"
  5. "go-common/library/cache/redis"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/rate/limit/bench/stress/conf"
  8. )
  9. // Dao dao
  10. type Dao struct {
  11. c *conf.Config
  12. mc *memcache.Pool
  13. redis *redis.Pool
  14. db *xsql.DB
  15. }
  16. // New init mysql db
  17. func New(c *conf.Config) (dao *Dao) {
  18. dao = &Dao{
  19. c: c,
  20. mc: memcache.NewPool(c.Memcache),
  21. redis: redis.NewPool(c.Redis),
  22. db: xsql.NewMySQL(c.MySQL),
  23. }
  24. return
  25. }
  26. // Close close the resource.
  27. func (d *Dao) Close() {
  28. d.mc.Close()
  29. d.redis.Close()
  30. d.db.Close()
  31. }
  32. // Ping dao ping
  33. func (d *Dao) Ping(c context.Context) error {
  34. return d.pingMC(c)
  35. }
  36. // pingMc ping
  37. func (d *Dao) pingMC(c context.Context) (err error) {
  38. conn := d.mc.Get(c)
  39. defer conn.Close()
  40. return
  41. }