dao.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/coin/conf"
  6. "go-common/app/service/main/coin/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/cache/redis"
  9. "go-common/library/database/elastic"
  10. "go-common/library/database/sql"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/queue/databus"
  13. "go-common/library/stat/prom"
  14. "go-common/library/sync/pipeline/fanout"
  15. )
  16. // Dao dao config.
  17. type Dao struct {
  18. c *conf.Config
  19. // databus stat
  20. stat *databus.Databus
  21. // coin
  22. coin *sql.DB
  23. //http
  24. httpClient *bm.Client
  25. // databus
  26. dbBigData *databus.Databus
  27. // databus for coin-job
  28. dbCoinJob *databus.Databus
  29. // redis
  30. redis *redis.Pool
  31. expireAdded int32
  32. expireExp int32
  33. // tag url
  34. tagURI string
  35. mc *memcache.Pool
  36. mcExpire int32
  37. cache *fanout.Fanout
  38. Businesses map[int64]*model.Business
  39. BusinessNames map[string]*model.Business
  40. es *elastic.Elastic
  41. }
  42. // New new a Dao and return.
  43. func New(c *conf.Config) (d *Dao) {
  44. d = &Dao{
  45. c: c,
  46. coin: sql.NewMySQL(c.DB.Coin),
  47. httpClient: bm.NewClient(c.HTTPClient),
  48. tagURI: c.TagURL,
  49. redis: redis.NewPool(c.Redis.Config),
  50. mc: memcache.NewPool(c.Memcache.Config),
  51. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  52. expireExp: int32(time.Duration(c.Memcache.ExpExpire) / time.Second),
  53. dbBigData: databus.New(c.DbBigData),
  54. dbCoinJob: databus.New(c.DbCoinJob),
  55. stat: databus.New(c.Stat.Databus),
  56. expireAdded: int32(time.Duration(c.Redis.Expire) / time.Second),
  57. cache: fanout.New("cache", fanout.Buffer(10240)),
  58. Businesses: make(map[int64]*model.Business),
  59. BusinessNames: make(map[string]*model.Business),
  60. es: elastic.NewElastic(nil),
  61. }
  62. if len(c.Businesses) > 0 {
  63. for _, b := range c.Businesses {
  64. d.Businesses[b.ID] = b
  65. d.BusinessNames[b.Name] = b
  66. }
  67. }
  68. return
  69. }
  70. // PromError .
  71. func PromError(name string) {
  72. prom.BusinessErrCount.Incr(name)
  73. }
  74. // Ping check service health.
  75. func (dao *Dao) Ping(c context.Context) (err error) {
  76. return dao.coin.Ping(c)
  77. }