dao.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/playlist/conf"
  6. "go-common/library/cache/redis"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/stat/prom"
  11. )
  12. // Dao .
  13. type Dao struct {
  14. c *conf.Config
  15. db *xsql.DB
  16. redis *redis.Pool
  17. httpClient *bm.Client
  18. viewCacheTTL int64
  19. }
  20. // New creates a dao instance.
  21. func New(c *conf.Config) (d *Dao) {
  22. d = &Dao{
  23. c: c,
  24. db: xsql.NewMySQL(c.Mysql),
  25. redis: redis.NewPool(c.Redis),
  26. httpClient: bm.NewClient(c.HTTPClient),
  27. viewCacheTTL: int64(time.Duration(c.Job.ViewCacheTTL) / time.Second),
  28. }
  29. return
  30. }
  31. // PromError stat and log.
  32. func PromError(name string, format string, args ...interface{}) {
  33. prom.BusinessErrCount.Incr(name)
  34. log.Error(format, args...)
  35. }
  36. // PromInfo prometheus info count.
  37. func PromInfo(name string) {
  38. prom.BusinessInfoCount.Incr(name)
  39. }
  40. // Ping reports the health of the db/cache etc.
  41. func (d *Dao) Ping(c context.Context) (err error) {
  42. if err = d.db.Ping(c); err != nil {
  43. return
  44. }
  45. err = d.pingRedis(c)
  46. return
  47. }
  48. func (d *Dao) pingRedis(c context.Context) (err error) {
  49. conn := d.redis.Get(c)
  50. _, err = conn.Do("SET", "PING", "PONG")
  51. conn.Close()
  52. return
  53. }