dao.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package dao
  2. import (
  3. "context"
  4. xhttp "net/http"
  5. "runtime"
  6. "time"
  7. "go-common/app/interface/openplatform/article/conf"
  8. "go-common/library/cache"
  9. "go-common/library/cache/memcache"
  10. xredis "go-common/library/cache/redis"
  11. "go-common/library/database/sql"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/queue/databus"
  15. "go-common/library/stat/prom"
  16. hbase "go-common/library/database/hbase.v2"
  17. )
  18. var (
  19. errorsCount = prom.BusinessErrCount
  20. infosCount = prom.BusinessInfoCount
  21. cachedCount = prom.CacheHit
  22. missedCount = prom.CacheMiss
  23. )
  24. // PromError prom error
  25. func PromError(name string) {
  26. errorsCount.Incr(name)
  27. }
  28. // promErrorCheck check prom error
  29. func promErrorCheck(err error) {
  30. if err != nil {
  31. pc, _, _, _ := runtime.Caller(1)
  32. name := "d." + runtime.FuncForPC(pc).Name()
  33. PromError(name)
  34. log.Error("%s err: %+v", name, err)
  35. }
  36. }
  37. // PromInfo add prom info
  38. func PromInfo(name string) {
  39. infosCount.Incr(name)
  40. }
  41. // Dao dao
  42. type Dao struct {
  43. // config
  44. c *conf.Config
  45. // db
  46. articleDB *sql.DB
  47. // http client
  48. httpClient *bm.Client
  49. messageHTTPClient *bm.Client
  50. bfsClient *xhttp.Client
  51. // memcache
  52. mc *memcache.Pool
  53. //redis
  54. redis *xredis.Pool
  55. mcArticleExpire int32
  56. mcStatsExpire int32
  57. mcLikeExpire int32
  58. mcCardsExpire int32
  59. mcListArtsExpire int32
  60. mcListExpire int32
  61. mcArtListExpire int32
  62. mcUpListsExpire int32
  63. mcSubExp int32
  64. mcListReadExpire int32
  65. mcHotspotExpire int32
  66. mcAuthorExpire int32
  67. mcArticlesIDExpire int32
  68. mcArticleTagExpire int32
  69. mcUpStatDailyExpire int32
  70. redisUpperExpire int32
  71. redisSortExpire int64
  72. redisSortTTL int64
  73. redisArtLikesExpire int32
  74. redisRankExpire int64
  75. redisRankTTL int64
  76. redisMaxLikeExpire int64
  77. redisHotspotExpire int64
  78. redisReadPingExpire int64
  79. redisReadSetExpire int64
  80. // stmt
  81. categoriesStmt *sql.Stmt
  82. authorsStmt *sql.Stmt
  83. applyStmt *sql.Stmt
  84. addAuthorStmt *sql.Stmt
  85. applyCountStmt *sql.Stmt
  86. articleMetaStmt *sql.Stmt
  87. allArticleMetaStmt *sql.Stmt
  88. articleContentStmt *sql.Stmt
  89. articleUpperCountStmt *sql.Stmt
  90. updateArticleStateStmt *sql.Stmt
  91. upPassedStmt *sql.Stmt
  92. recommendCategoryStmt *sql.Stmt
  93. delRecommendStmt *sql.Stmt
  94. allRecommendStmt *sql.Stmt
  95. allRecommendCountStmt *sql.Stmt
  96. newestArtsMetaStmt *sql.Stmt
  97. upperArtCntCreationStmt *sql.Stmt
  98. articleMetaCreationStmt *sql.Stmt
  99. articleUpCntTodayStmt *sql.Stmt
  100. addComplaintStmt *sql.Stmt
  101. complaintExistStmt *sql.Stmt
  102. complaintProtectStmt *sql.Stmt
  103. addComplaintCountStmt *sql.Stmt
  104. settingsStmt *sql.Stmt
  105. authorStmt *sql.Stmt
  106. noticeStmt *sql.Stmt
  107. userNoticeStmt *sql.Stmt
  108. updateUserNoticeStmt *sql.Stmt
  109. creativeListsStmt *sql.Stmt
  110. creativeListAddStmt *sql.Stmt
  111. creativeListDelStmt *sql.Stmt
  112. creativeListUpdateStmt *sql.Stmt
  113. creativeListUpdateTimeStmt *sql.Stmt
  114. creativeListArticlesStmt *sql.Stmt
  115. listStmt *sql.Stmt
  116. creativeListDelAllArticleStmt *sql.Stmt
  117. creativeListAddArticleStmt *sql.Stmt
  118. creativeListDelArticleStmt *sql.Stmt
  119. allListStmt *sql.Stmt
  120. hotspotsStmt *sql.Stmt
  121. searchArtsStmt *sql.Stmt
  122. addCheatStmt *sql.Stmt
  123. delCheatStmt *sql.Stmt
  124. // databus
  125. statDbus *databus.Databus
  126. // inteval
  127. UpdateRecommendsInterval int64
  128. UpdateBannersInterval int64
  129. // hbase
  130. hbase *hbase.Client
  131. //cache
  132. cache *cache.Cache
  133. }
  134. // New dao new
  135. func New(c *conf.Config) (d *Dao) {
  136. d = &Dao{
  137. // config
  138. c: c,
  139. // http client
  140. httpClient: bm.NewClient(c.HTTPClient),
  141. messageHTTPClient: bm.NewClient(c.MessageHTTPClient),
  142. bfsClient: &xhttp.Client{Timeout: time.Duration(c.BFS.Timeout)},
  143. // mc
  144. mc: memcache.NewPool(c.Memcache.Config),
  145. mcArticleExpire: int32(time.Duration(c.Memcache.ArticleExpire) / time.Second),
  146. mcStatsExpire: int32(time.Duration(c.Memcache.StatsExpire) / time.Second),
  147. mcLikeExpire: int32(time.Duration(c.Memcache.LikeExpire) / time.Second),
  148. mcCardsExpire: int32(time.Duration(c.Memcache.CardsExpire) / time.Second),
  149. mcSubExp: int32(time.Duration(c.Memcache.SubmitExpire) / time.Second),
  150. mcListArtsExpire: int32(time.Duration(c.Memcache.ListArtsExpire) / time.Second),
  151. mcListExpire: int32(time.Duration(c.Memcache.ListExpire) / time.Second),
  152. mcArtListExpire: int32(time.Duration(c.Memcache.ArtListExpire) / time.Second),
  153. mcUpListsExpire: int32(time.Duration(c.Memcache.UpListsExpire) / time.Second),
  154. mcListReadExpire: int32(time.Duration(c.Memcache.ListReadExpire) / time.Second),
  155. mcHotspotExpire: int32(time.Duration(c.Memcache.HotspotExpire) / time.Second),
  156. mcAuthorExpire: int32(time.Duration(c.Memcache.AuthorExpire) / time.Second),
  157. mcArticlesIDExpire: int32(time.Duration(c.Memcache.ArticlesIDExpire) / time.Second),
  158. mcArticleTagExpire: int32(time.Duration(c.Memcache.ArticleTagExpire) / time.Second),
  159. mcUpStatDailyExpire: int32(time.Duration(c.Memcache.UpStatDailyExpire) / time.Second),
  160. //redis
  161. redis: xredis.NewPool(c.Redis),
  162. redisUpperExpire: int32(time.Duration(c.Article.ExpireUpper) / time.Second),
  163. redisSortExpire: int64(time.Duration(c.Article.ExpireSortArts) / time.Second),
  164. redisSortTTL: int64(time.Duration(c.Article.TTLSortArts) / time.Second),
  165. redisArtLikesExpire: int32(time.Duration(c.Article.ExpireArtLikes) / time.Second),
  166. redisRankExpire: int64(time.Duration(c.Article.ExpireRank) / time.Second),
  167. redisRankTTL: int64(time.Duration(c.Article.TTLRank) / time.Second),
  168. redisMaxLikeExpire: int64(time.Duration(c.Article.ExpireMaxLike) / time.Second),
  169. redisHotspotExpire: int64(time.Duration(c.Article.ExpireHotspot) / time.Second),
  170. redisReadPingExpire: int64(time.Duration(c.Article.ExpireReadPing) / time.Second),
  171. redisReadSetExpire: int64(time.Duration(c.Article.ExpireReadSet) / time.Second),
  172. // db
  173. articleDB: sql.NewMySQL(c.MySQL.Article),
  174. // prom
  175. statDbus: databus.New(c.StatDatabus),
  176. UpdateRecommendsInterval: int64(time.Duration(c.Article.UpdateRecommendsInteval) / time.Second),
  177. UpdateBannersInterval: int64(time.Duration(c.Article.UpdateBannersInteval) / time.Second),
  178. // hbase
  179. hbase: hbase.NewClient(c.HBase),
  180. cache: cache.New(1, 1024),
  181. }
  182. d.categoriesStmt = d.articleDB.Prepared(_categoriesSQL)
  183. d.authorsStmt = d.articleDB.Prepared(_authorsSQL)
  184. d.applyStmt = d.articleDB.Prepared(_applySQL)
  185. d.addAuthorStmt = d.articleDB.Prepared(_addAuthorSQL)
  186. d.applyCountStmt = d.articleDB.Prepared(_applyCountSQL)
  187. d.articleMetaStmt = d.articleDB.Prepared(_articleMetaSQL)
  188. d.allArticleMetaStmt = d.articleDB.Prepared(_allArticleMetaSQL)
  189. d.articleContentStmt = d.articleDB.Prepared(_articleContentSQL)
  190. d.updateArticleStateStmt = d.articleDB.Prepared(_updateArticleStateSQL)
  191. d.upPassedStmt = d.articleDB.Prepared(_upperPassedSQL)
  192. d.recommendCategoryStmt = d.articleDB.Prepared(_recommendCategorySQL)
  193. d.allRecommendStmt = d.articleDB.Prepared(_allRecommendSQL)
  194. d.allRecommendCountStmt = d.articleDB.Prepared(_allRecommendCountSQL)
  195. d.delRecommendStmt = d.articleDB.Prepared(_deleteRecommendSQL)
  196. d.newestArtsMetaStmt = d.articleDB.Prepared(_newestArtsMetaSQL)
  197. d.upperArtCntCreationStmt = d.articleDB.Prepared(_upperArticleCountCreationSQL)
  198. d.articleUpperCountStmt = d.articleDB.Prepared(_articleUpperCountSQL)
  199. d.articleMetaCreationStmt = d.articleDB.Prepared(_articleMetaCreationSQL)
  200. d.articleUpCntTodayStmt = d.articleDB.Prepared(_articleUpCntTodaySQL)
  201. d.addComplaintStmt = d.articleDB.Prepared(_addComplaintsSQL)
  202. d.complaintExistStmt = d.articleDB.Prepared(_complaintExistSQL)
  203. d.complaintProtectStmt = d.articleDB.Prepared(_complaintProtectSQL)
  204. d.addComplaintCountStmt = d.articleDB.Prepared(_addComplaintCountSQL)
  205. d.settingsStmt = d.articleDB.Prepared(_settingsSQL)
  206. d.authorStmt = d.articleDB.Prepared(_authorSQL)
  207. d.noticeStmt = d.articleDB.Prepared(_noticeSQL)
  208. d.userNoticeStmt = d.articleDB.Prepared(_userNoticeSQL)
  209. d.updateUserNoticeStmt = d.articleDB.Prepared(_updateUserNoticeSQL)
  210. d.creativeListsStmt = d.articleDB.Prepared(_creativeListsSQL)
  211. d.creativeListAddStmt = d.articleDB.Prepared(_creativeListAddSQL)
  212. d.creativeListDelStmt = d.articleDB.Prepared(_creativeListDelSQL)
  213. d.creativeListUpdateStmt = d.articleDB.Prepared(_creativeListUpdateSQL)
  214. d.creativeListUpdateTimeStmt = d.articleDB.Prepared(_creativeListUpdateTimeSQL)
  215. d.creativeListArticlesStmt = d.articleDB.Prepared(_creativeListArticlesSQL)
  216. d.creativeListDelAllArticleStmt = d.articleDB.Prepared(_creativeListDelAllArticleSQL)
  217. d.creativeListAddArticleStmt = d.articleDB.Prepared(_creativeListAddArticleSQL)
  218. d.listStmt = d.articleDB.Prepared(_listSQL)
  219. d.creativeListDelArticleStmt = d.articleDB.Prepared(_creativeListDelArticleSQL)
  220. d.allListStmt = d.articleDB.Prepared(_allListsSQL)
  221. d.hotspotsStmt = d.articleDB.Prepared(_hotspotsSQL)
  222. d.searchArtsStmt = d.articleDB.Prepared(_searchArticles)
  223. d.addCheatStmt = d.articleDB.Prepared(_addCheatSQL)
  224. d.delCheatStmt = d.articleDB.Prepared(_delCheatSQL)
  225. return d
  226. }
  227. // BeginTran begin transaction.
  228. func (d *Dao) BeginTran(c context.Context) (*sql.Tx, error) {
  229. return d.articleDB.Begin(c)
  230. }
  231. // Ping check connection success.
  232. func (d *Dao) Ping(c context.Context) (err error) {
  233. if err = d.pingMC(c); err != nil {
  234. PromError("mc:Ping")
  235. log.Error("d.pingMC error(%+v)", err)
  236. return
  237. }
  238. if err = d.pingRedis(c); err != nil {
  239. PromError("redis:Ping")
  240. log.Error("d.pingRedis error(%+v)", err)
  241. return
  242. }
  243. if err = d.articleDB.Ping(c); err != nil {
  244. PromError("db:Ping")
  245. log.Error("d.articleDB.Ping error(%+v)", err)
  246. }
  247. return
  248. }
  249. // Close close resource.
  250. func (d *Dao) Close() {
  251. d.articleDB.Close()
  252. d.mc.Close()
  253. d.redis.Close()
  254. }