dao.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/feed/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/stat/prom"
  11. )
  12. var (
  13. // CachedCount .
  14. CachedCount = prom.CacheHit
  15. // MissedCount .
  16. MissedCount = prom.CacheMiss
  17. infosCount = prom.BusinessInfoCount
  18. warnsCount = prom.BusinessErrCount
  19. )
  20. // PromError stat and log.
  21. func PromError(name string, format string, args ...interface{}) {
  22. prom.BusinessErrCount.Incr(name)
  23. log.Error(format, args...)
  24. }
  25. // PromInfo add prom info
  26. func PromInfo(name string) {
  27. infosCount.Incr(name)
  28. }
  29. // PromWarn add prom warn
  30. func PromWarn(name string) {
  31. warnsCount.Incr(name)
  32. }
  33. // Dao struct info of Dao.
  34. type Dao struct {
  35. // redis
  36. redis *redis.Pool
  37. redisTTLUpper int32
  38. redisExpireUpper int32
  39. redisExpireFeed int32
  40. redisExpireArchiveFeed int32
  41. redisExpireBangumiFeed int32
  42. // memcache
  43. mc *memcache.Pool
  44. mcExpire int32
  45. bangumiExpire int32
  46. // feed Config
  47. appFeedLength int
  48. webFeedLength int
  49. // conf
  50. c *conf.Config
  51. // bangumi http client
  52. httpClient *bm.Client
  53. }
  54. // New new a Dao and return.
  55. func New(c *conf.Config) (d *Dao) {
  56. d = &Dao{
  57. // conf
  58. c: c,
  59. // redis
  60. redis: redis.NewPool(c.MultiRedis.Cache),
  61. redisTTLUpper: int32(time.Duration(c.MultiRedis.TTLUpper) / time.Second),
  62. redisExpireUpper: int32(time.Duration(c.MultiRedis.ExpireUpper) / time.Second),
  63. redisExpireFeed: int32(time.Duration(c.MultiRedis.ExpireFeed) / time.Second),
  64. redisExpireArchiveFeed: int32(time.Duration(c.Feed.ArchiveFeedExpire) / time.Second),
  65. redisExpireBangumiFeed: int32(time.Duration(c.Feed.BangumiFeedExpire) / time.Second),
  66. // mc
  67. mc: memcache.NewPool(c.Memcache.Config),
  68. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  69. bangumiExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  70. // feed Config
  71. appFeedLength: c.Feed.AppLength,
  72. webFeedLength: c.Feed.WebLength,
  73. httpClient: bm.NewClient(c.HTTPClient),
  74. }
  75. if d.appFeedLength == 0 {
  76. d.appFeedLength = 200
  77. }
  78. if d.webFeedLength == 0 {
  79. d.webFeedLength = 400
  80. }
  81. return
  82. }
  83. // Ping ping health of redis and mc.
  84. func (d *Dao) Ping(c context.Context) (err error) {
  85. if err = d.pingRedis(c); err != nil {
  86. return
  87. }
  88. return d.pingMC(c)
  89. }
  90. // Close close connections of redis and mc.
  91. func (d *Dao) Close() {
  92. if d.redis != nil {
  93. d.redis.Close()
  94. }
  95. if d.mc != nil {
  96. d.mc.Close()
  97. }
  98. }