dao.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/playlist/conf"
  7. "go-common/library/cache/redis"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/queue/databus"
  12. "go-common/library/stat/prom"
  13. )
  14. // Dao dao struct.
  15. type Dao struct {
  16. // config
  17. c *conf.Config
  18. // db
  19. db *sql.DB
  20. // redis
  21. redis *redis.Pool
  22. statExpire int32
  23. plExpire int32
  24. // http client
  25. http *bm.Client
  26. // stmt
  27. videosStmt map[string]*sql.Stmt
  28. // databus
  29. viewDbus *databus.Databus
  30. shareDbus *databus.Databus
  31. // search video URL
  32. searchURL string
  33. }
  34. // New new dao.
  35. func New(c *conf.Config) (d *Dao) {
  36. d = &Dao{
  37. // config
  38. c: c,
  39. db: sql.NewMySQL(c.Mysql),
  40. redis: redis.NewPool(c.Redis.Config),
  41. statExpire: int32(time.Duration(c.Redis.StatExpire) / time.Second),
  42. plExpire: int32(time.Duration(c.Redis.PlExpire) / time.Second),
  43. http: bm.NewClient(c.HTTPClient),
  44. viewDbus: databus.New(c.ViewDatabus),
  45. shareDbus: databus.New(c.ShareDatabus),
  46. searchURL: c.Host.Search + _searchURL,
  47. }
  48. d.videosStmt = make(map[string]*sql.Stmt, _plArcSub)
  49. for i := 0; i < _plArcSub; i++ {
  50. key := fmt.Sprintf("%02d", i)
  51. d.videosStmt[key] = d.db.Prepared(fmt.Sprintf(_plArcsSQL, key))
  52. }
  53. return
  54. }
  55. // Ping ping dao
  56. func (d *Dao) Ping(c context.Context) (err error) {
  57. if err = d.db.Ping(c); err != nil {
  58. return
  59. }
  60. err = d.pingRedis(c)
  61. return
  62. }
  63. func (d *Dao) pingRedis(c context.Context) (err error) {
  64. conn := d.redis.Get(c)
  65. _, err = conn.Do("SET", "PING", "PONG")
  66. conn.Close()
  67. return
  68. }
  69. // PromError stat and log.
  70. func PromError(name string, format string, args ...interface{}) {
  71. prom.BusinessErrCount.Incr(name)
  72. log.Error(format, args...)
  73. }