dao.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/dynamic/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/stat/prom"
  10. )
  11. const (
  12. _regionURL = "/dynamic/region"
  13. _regionTagURL = "/dynamic/tag"
  14. _liveURL = "/room/v1/Area/dynamic"
  15. _hotURL = "/x/internal/tag/hotmap"
  16. _pridURL = "/x/internal/tag/prids"
  17. )
  18. // PromError stat and log.
  19. func PromError(name string, format string, args ...interface{}) {
  20. prom.BusinessErrCount.Incr(name)
  21. log.Error(format, args...)
  22. }
  23. // Dao dao.
  24. type Dao struct {
  25. // http
  26. httpR *bm.Client
  27. // bigData api
  28. regionURI string
  29. regionTagURI string
  30. // live api
  31. liveURI string
  32. // tag api
  33. hotURI string
  34. pridURI string
  35. // memcache
  36. mc *memcache.Pool
  37. mcExpire int32
  38. // cache Prom
  39. cacheProm *prom.Prom
  40. }
  41. // New dao new.
  42. func New(c *conf.Config) (d *Dao) {
  43. d = &Dao{
  44. httpR: bm.NewClient(c.HTTPClient.Read),
  45. regionURI: c.Host.BigDataURI + _regionURL,
  46. regionTagURI: c.Host.BigDataURI + _regionTagURL,
  47. liveURI: c.Host.LiveURI + _liveURL,
  48. hotURI: c.Host.APIURI + _hotURL,
  49. pridURI: c.Host.APIURI + _pridURL,
  50. // memcache
  51. mc: memcache.NewPool(c.Memcache.Config),
  52. mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
  53. }
  54. d.cacheProm = prom.CacheHit
  55. return
  56. }
  57. // Ping check connection success.
  58. func (dao *Dao) Ping(c context.Context) (err error) {
  59. err = dao.pingMC(c)
  60. return
  61. }
  62. // Close close memcache resource.
  63. func (dao *Dao) Close() {
  64. if dao.mc != nil {
  65. dao.mc.Close()
  66. }
  67. }