dao.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/openplatform/article/conf"
  6. "go-common/library/cache"
  7. "go-common/library/cache/redis"
  8. xsql "go-common/library/database/sql"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/queue/databus"
  11. "go-common/library/stat/prom"
  12. )
  13. // Dao .
  14. type Dao struct {
  15. c *conf.Config
  16. db *xsql.DB
  17. redis *redis.Pool
  18. artRedis *redis.Pool
  19. httpClient *bm.Client
  20. gameHTTPClient *bm.Client
  21. viewCacheTTL, gameCacheExpire int64
  22. dupViewCacheTTL int64
  23. redisSortExpire int64
  24. redisSortTTL int64
  25. // stmt
  26. updateSearchStmt *xsql.Stmt
  27. delSearchStmt *xsql.Stmt
  28. updateSearchStatsStmt *xsql.Stmt
  29. gameStmt *xsql.Stmt
  30. cheatStmt *xsql.Stmt
  31. newestArtsMetaStmt *xsql.Stmt
  32. searchArtsStmt *xsql.Stmt
  33. updateRecheckStmt *xsql.Stmt
  34. getRecheckStmt *xsql.Stmt
  35. settingsStmt *xsql.Stmt
  36. midByPubtimeStmt *xsql.Stmt
  37. statByMidStmt *xsql.Stmt
  38. dynamicDbus *databus.Databus
  39. cache *cache.Cache
  40. }
  41. var (
  42. errorsCount = prom.BusinessErrCount
  43. cacheLen = prom.BusinessInfoCount
  44. infosCount = prom.BusinessInfoCount
  45. )
  46. // New creates a dao instance.
  47. func New(c *conf.Config) (d *Dao) {
  48. d = &Dao{
  49. c: c,
  50. db: xsql.NewMySQL(c.DB),
  51. redis: redis.NewPool(c.Redis),
  52. artRedis: redis.NewPool(c.ArtRedis),
  53. httpClient: bm.NewClient(c.HTTPClient),
  54. gameHTTPClient: bm.NewClient(c.GameHTTPClient),
  55. viewCacheTTL: int64(time.Duration(c.Job.ViewCacheTTL) / time.Second),
  56. dupViewCacheTTL: int64(time.Duration(c.Job.DupViewCacheTTL) / time.Second),
  57. gameCacheExpire: int64(time.Duration(c.Job.GameCacheExpire) / time.Second),
  58. redisSortExpire: int64(time.Duration(c.Job.ExpireSortArts) / time.Second),
  59. redisSortTTL: int64(time.Duration(c.Job.TTLSortArts) / time.Second),
  60. dynamicDbus: databus.New(c.DynamicDbus),
  61. cache: cache.New(1, 1024),
  62. }
  63. d.updateSearchStmt = d.db.Prepared(_updateSearch)
  64. d.delSearchStmt = d.db.Prepared(_delSearch)
  65. d.updateSearchStatsStmt = d.db.Prepared(_updateSearchStats)
  66. d.newestArtsMetaStmt = d.db.Prepared(_newestArtsMetaSQL)
  67. d.gameStmt = d.db.Prepared(_gameList)
  68. d.cheatStmt = d.db.Prepared(_allCheat)
  69. d.searchArtsStmt = d.db.Prepared(_searchArticles)
  70. d.updateRecheckStmt = d.db.Prepared(_updateCheckState)
  71. d.getRecheckStmt = d.db.Prepared(_checkStateSQL)
  72. d.settingsStmt = d.db.Prepared(_settingsSQL)
  73. d.midByPubtimeStmt = d.db.Prepared(_midsByPublishTimeSQL)
  74. d.statByMidStmt = d.db.Prepared(_statByMidSQL)
  75. return
  76. }
  77. //go:generate $GOPATH/src/go-common/app/tool/cache/gen
  78. type _cache interface {
  79. GameList(c context.Context) (res []int64, err error)
  80. }
  81. // PromError prometheus error count.
  82. func PromError(name string) {
  83. errorsCount.Incr(name)
  84. }
  85. // PromInfo prometheus info count.
  86. func PromInfo(name string) {
  87. infosCount.Incr(name)
  88. }
  89. // Ping reports the health of the db/cache etc.
  90. func (d *Dao) Ping(c context.Context) (err error) {
  91. if err = d.db.Ping(c); err != nil {
  92. PromError("db:Ping")
  93. return
  94. }
  95. err = d.pingRedis(c)
  96. return
  97. }