dao.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/relation-cache/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. xsql "go-common/library/database/sql"
  9. bm "go-common/library/net/http/blademaster"
  10. xtime "go-common/library/time"
  11. )
  12. // Dao dao
  13. type Dao struct {
  14. *cacheTTL
  15. c *conf.Config
  16. mc *memcache.Pool
  17. redis *redis.Pool
  18. db *xsql.DB
  19. client *bm.Client
  20. }
  21. // New init mysql db
  22. func New(c *conf.Config) (dao *Dao) {
  23. dao = &Dao{
  24. cacheTTL: &cacheTTL{
  25. RelationTTL: asSecond(c.CacheTTL.RelationTTL),
  26. },
  27. c: c,
  28. mc: memcache.NewPool(c.Memcache),
  29. redis: redis.NewPool(c.Redis),
  30. db: xsql.NewMySQL(c.MySQL),
  31. client: bm.NewClient(c.HTTPClient),
  32. }
  33. return
  34. }
  35. type cacheTTL struct {
  36. RelationTTL int64
  37. }
  38. func asSecond(d xtime.Duration) int64 {
  39. return int64(time.Duration(d) / time.Second)
  40. }
  41. // Close close the resource.
  42. func (d *Dao) Close() {
  43. d.mc.Close()
  44. d.redis.Close()
  45. d.db.Close()
  46. }
  47. // Ping dao ping
  48. func (d *Dao) Ping(c context.Context) error {
  49. // TODO: if you need use mc,redis, please add
  50. return d.db.Ping(c)
  51. }