dao.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/space/conf"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/cache/redis"
  9. "go-common/library/database/sql"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/sync/pipeline/fanout"
  12. "go-common/library/database/hbase.v2"
  13. )
  14. // Dao dao struct.
  15. type Dao struct {
  16. // config
  17. c *conf.Config
  18. // db
  19. db *sql.DB
  20. // hbase
  21. hbase *hbase.Client
  22. // stmt
  23. channelStmt []*sql.Stmt
  24. channelListStmt []*sql.Stmt
  25. channelCntStmt []*sql.Stmt
  26. channelArcCntStmt []*sql.Stmt
  27. // redis
  28. redis *redis.Pool
  29. // mc
  30. mc *memcache.Pool
  31. // http client
  32. httpR *bm.Client
  33. httpW *bm.Client
  34. httpGame *bm.Client
  35. // api URL
  36. bangumiURL string
  37. bangumiConcernURL string
  38. bangumiUnConcernURL string
  39. favFolderURL string
  40. favArcURL string
  41. favAlbumURL string
  42. favMovieURL string
  43. shopURL string
  44. shopLinkURL string
  45. albumCountURL string
  46. albumListURL string
  47. tagSubURL string
  48. tagCancelSubURL string
  49. tagSubListURL string
  50. accTagsURL string
  51. accTagsSetURL string
  52. isAnsweredURL string
  53. lastPlayGameURL string
  54. appPlayedGameURL string
  55. arcSearchURL string
  56. webTopPhotoURL string
  57. topPhotoURL string
  58. liveMetalURL string
  59. liveURL string
  60. medalStatusURL string
  61. groupsCountURL string
  62. elecURL string
  63. audioCardURL string
  64. audioUpperCertURL string
  65. audioCntURL string
  66. dynamicListURL string
  67. dynamicURL string
  68. dynamicCntURL string
  69. // expire
  70. clExpire int32
  71. upArtExpire int32
  72. upArcExpire int32
  73. mcSettingExpire int32
  74. mcNoticeExpire int32
  75. mcTopArcExpire int32
  76. mcMpExpire int32
  77. mcThemeExpire int32
  78. mcTopDyExpire int32
  79. // cache
  80. cache *fanout.Fanout
  81. }
  82. // New new dao.
  83. func New(c *conf.Config) (d *Dao) {
  84. d = &Dao{
  85. // config
  86. c: c,
  87. db: sql.NewMySQL(c.Mysql),
  88. hbase: hbase.NewClient(c.HBase.Config),
  89. redis: redis.NewPool(c.Redis.Config),
  90. mc: memcache.NewPool(c.Memcache.Config),
  91. httpR: bm.NewClient(c.HTTPClient.Read),
  92. httpW: bm.NewClient(c.HTTPClient.Write),
  93. httpGame: bm.NewClient(c.HTTPClient.Game),
  94. bangumiURL: c.Host.Bangumi + _bangumiURI,
  95. bangumiConcernURL: c.Host.Bangumi + _bangumiConcernURI,
  96. bangumiUnConcernURL: c.Host.Bangumi + _bangumiUnConcernURI,
  97. favFolderURL: c.Host.API + _favFolderURI,
  98. favArcURL: c.Host.API + _favArchiveURI,
  99. favAlbumURL: c.Host.APILive + _favAlbumURI,
  100. favMovieURL: c.Host.Bangumi + _favMovieURI,
  101. shopURL: c.Host.Mall + _shopURI,
  102. shopLinkURL: c.Host.Mall + _shopLinkURI,
  103. albumCountURL: c.Host.APIVc + _albumCountURI,
  104. albumListURL: c.Host.APIVc + _albumListURI,
  105. tagSubURL: c.Host.API + _tagSubURI,
  106. tagCancelSubURL: c.Host.API + _tagCancelSubURI,
  107. tagSubListURL: c.Host.API + _subTagListURI,
  108. accTagsURL: c.Host.Acc + _accTagsURI,
  109. accTagsSetURL: c.Host.Acc + _accTagsSetURI,
  110. isAnsweredURL: c.Host.API + _isAnsweredURI,
  111. lastPlayGameURL: c.Host.Game + _lastPlayGameURI,
  112. appPlayedGameURL: c.Host.AppGame + _appPlayedGameURI,
  113. arcSearchURL: c.Host.Search + _arcSearchURI,
  114. webTopPhotoURL: c.Host.Space + _webTopPhotoURI,
  115. topPhotoURL: c.Host.Space + _topPhotoURI,
  116. liveMetalURL: c.Host.APILive + _liveMetalURI,
  117. liveURL: c.Host.APILive + _liveURI,
  118. medalStatusURL: c.Host.APILive + _medalStatusURI,
  119. groupsCountURL: c.Host.APIVc + _groupsCountURI,
  120. elecURL: c.Host.Elec + _elecURI,
  121. audioCardURL: c.Host.API + _audioCardURI,
  122. audioUpperCertURL: c.Host.API + _audioUpperCertURI,
  123. audioCntURL: c.Host.API + _audioCntURI,
  124. dynamicListURL: c.Host.APIVc + _dynamicListURI,
  125. dynamicURL: c.Host.APIVc + _dynamicURI,
  126. dynamicCntURL: c.Host.APIVc + _dynamicCntURI,
  127. // expire
  128. clExpire: int32(time.Duration(c.Redis.ClExpire) / time.Second),
  129. upArtExpire: int32(time.Duration(c.Redis.UpArtExpire) / time.Second),
  130. upArcExpire: int32(time.Duration(c.Redis.UpArcExpire) / time.Second),
  131. mcSettingExpire: int32(time.Duration(c.Memcache.SettingExpire) / time.Second),
  132. mcNoticeExpire: int32(time.Duration(c.Memcache.NoticeExpire) / time.Second),
  133. mcTopArcExpire: int32(time.Duration(c.Memcache.TopArcExpire) / time.Second),
  134. mcMpExpire: int32(time.Duration(c.Memcache.MpExpire) / time.Second),
  135. mcThemeExpire: int32(time.Duration(c.Memcache.ThemeExpire) / time.Second),
  136. mcTopDyExpire: int32(time.Duration(c.Memcache.TopDyExpire) / time.Second),
  137. // cache
  138. cache: fanout.New("cache"),
  139. }
  140. d.channelStmt = make([]*sql.Stmt, _chSub)
  141. d.channelListStmt = make([]*sql.Stmt, _chSub)
  142. d.channelCntStmt = make([]*sql.Stmt, _chSub)
  143. d.channelArcCntStmt = make([]*sql.Stmt, _chSub)
  144. for i := 0; i < _chSub; i++ {
  145. d.channelStmt[i] = d.db.Prepared(fmt.Sprintf(_chSQL, i))
  146. d.channelListStmt[i] = d.db.Prepared(fmt.Sprintf(_chListSQL, i))
  147. d.channelCntStmt[i] = d.db.Prepared(fmt.Sprintf(_chCntSQL, i))
  148. d.channelArcCntStmt[i] = d.db.Prepared(fmt.Sprintf(_chArcCntSQL, i))
  149. }
  150. return
  151. }
  152. // Ping ping dao
  153. func (d *Dao) Ping(c context.Context) (err error) {
  154. if err = d.db.Ping(c); err != nil {
  155. return
  156. }
  157. err = d.pingRedis(c)
  158. return
  159. }
  160. func (d *Dao) pingRedis(c context.Context) (err error) {
  161. conn := d.redis.Get(c)
  162. _, err = conn.Do("SET", "PING", "PONG")
  163. conn.Close()
  164. return
  165. }