dao.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package weeklyhonor
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/creative/conf"
  6. up "go-common/app/service/main/up/api/v1"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/database/hbase.v2"
  9. "go-common/library/database/sql"
  10. "go-common/library/log"
  11. )
  12. // Dao is data dao.
  13. type Dao struct {
  14. c *conf.Config
  15. // hbase
  16. hbase *hbase.Client
  17. hbaseTimeOut time.Duration
  18. // db
  19. db *sql.DB
  20. // mc
  21. mc *memcache.Pool
  22. mcExpire int32
  23. mcClickExpire int32
  24. // grpc
  25. upClient up.UpClient
  26. }
  27. // New init dao
  28. func New(c *conf.Config) (d *Dao) {
  29. d = &Dao{
  30. c: c,
  31. // hbase
  32. hbase: hbase.NewClient(c.HBaseOld.Config),
  33. hbaseTimeOut: time.Duration(time.Millisecond * 200),
  34. // db
  35. db: sql.NewMySQL(c.DB.Creative),
  36. // mc
  37. mc: memcache.NewPool(c.Memcache.Honor.Config),
  38. mcExpire: int32(time.Duration(c.Memcache.Honor.HonorExpire) / time.Second),
  39. mcClickExpire: int32(time.Duration(c.Memcache.Honor.ClickExpire) / time.Second),
  40. }
  41. var err error
  42. if d.upClient, err = up.NewClient(c.UpClient); err != nil {
  43. panic(err)
  44. }
  45. return
  46. }
  47. // Ping ping success.
  48. func (d *Dao) Ping(c context.Context) (err error) {
  49. if err = d.pingMySQL(c); err != nil {
  50. log.Error("s.pingMySQL.Ping err(%v)", err)
  51. }
  52. return
  53. }
  54. // Close hbase close
  55. func (d *Dao) Close() (err error) {
  56. if d.hbase != nil {
  57. d.hbase.Close()
  58. }
  59. if d.db != nil {
  60. d.db.Close()
  61. }
  62. return
  63. }