dao.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package newbiedao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/growup/conf"
  6. "go-common/app/interface/main/growup/model"
  7. accApi "go-common/app/service/main/account/api"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. )
  12. var (
  13. // Categories cache categories
  14. Categories map[int64]*model.Category
  15. // RecommendUpList cache recommend up list
  16. RecommendUpList map[int64]map[int64]*model.RecommendUp
  17. )
  18. // Dao def dao struct
  19. type Dao struct {
  20. c *conf.Config
  21. db *sql.DB
  22. // search
  23. httpRead *bm.Client
  24. // grpc
  25. accGRPC accApi.AccountClient
  26. }
  27. // New fn
  28. func New(c *conf.Config) (d *Dao) {
  29. d = &Dao{
  30. c: c,
  31. db: sql.NewMySQL(c.DB.Growup),
  32. // search
  33. httpRead: bm.NewClient(c.HTTPClient.Read),
  34. }
  35. var err error
  36. if d.accGRPC, err = accApi.NewClient(c.AccCliConf); err != nil {
  37. panic(err)
  38. }
  39. d.loadCache()
  40. go func() {
  41. t := time.Tick(10 * time.Minute)
  42. for {
  43. d.loadCache()
  44. <-t
  45. }
  46. }()
  47. return d
  48. }
  49. // Ping ping db
  50. func (d *Dao) Ping(c context.Context) (err error) {
  51. if err = d.db.Ping(c); err != nil {
  52. log.Error("d.db.Ping error(%v)", err)
  53. return
  54. }
  55. return
  56. }
  57. // Close close db conn
  58. func (d *Dao) Close() {
  59. if d.db != nil {
  60. d.db.Close()
  61. }
  62. }
  63. // loodCache load cache
  64. func (d *Dao) loadCache() {
  65. _ = d.GetCategories(context.Background())
  66. log.Info("refresh categories cache: %+v", Categories)
  67. _ = d.GetRecommendUpList(context.Background())
  68. log.Info("refresh recommend up list cache: %+v", RecommendUpList)
  69. }