dao.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package dao
  2. import (
  3. "net/http"
  4. "time"
  5. "go-common/app/admin/main/tv/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/database/elastic"
  8. "go-common/library/database/orm"
  9. httpx "go-common/library/net/http/blademaster"
  10. "github.com/jinzhu/gorm"
  11. )
  12. // Dao struct user of Dao.
  13. type Dao struct {
  14. c *conf.Config
  15. // db
  16. DB *gorm.DB
  17. // dbshow
  18. DBShow *gorm.DB
  19. fullURL string
  20. httpSearch *httpx.Client
  21. client *httpx.Client
  22. bfsClient *http.Client
  23. esClient *elastic.Elastic
  24. // memcache
  25. mc *memcache.Pool
  26. cmsExpire int32
  27. }
  28. // New create a instance of Dao and return.
  29. func New(c *conf.Config) (d *Dao) {
  30. d = &Dao{
  31. // conf
  32. c: c,
  33. // db
  34. DB: orm.NewMySQL(c.ORM),
  35. // dbshow
  36. DBShow: orm.NewMySQL(c.ORMShow),
  37. // http client
  38. fullURL: c.HTTPSearch.FullURL,
  39. httpSearch: httpx.NewClient(c.HTTPSearch.ClientConfig),
  40. client: httpx.NewClient(c.HTTPClient),
  41. bfsClient: &http.Client{Timeout: time.Duration(c.Bfs.Timeout) * time.Millisecond},
  42. esClient: elastic.NewElastic(&elastic.Config{
  43. Host: c.Cfg.Hosts.Manager,
  44. HTTPClient: c.HTTPClient,
  45. }),
  46. mc: memcache.NewPool(c.Memcache.Config),
  47. cmsExpire: int32(time.Duration(c.Memcache.CmsExpire) / time.Second),
  48. }
  49. d.initORM()
  50. return
  51. }
  52. func (d *Dao) initORM() {
  53. d.DB.LogMode(true)
  54. d.DBShow.LogMode(true)
  55. }