dao.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. v1 "go-common/app/service/main/account/api"
  8. "go-common/app/service/main/account/conf"
  9. member "go-common/app/service/main/member/api/gorpc"
  10. mmodel "go-common/app/service/main/member/model"
  11. usersuit "go-common/app/service/main/usersuit/rpc/client"
  12. "go-common/library/cache/memcache"
  13. "go-common/library/database/elastic"
  14. bm "go-common/library/net/http/blademaster"
  15. "go-common/library/sync/pipeline/fanout"
  16. )
  17. //go:generate $GOPATH/src/go-common/app/tool/cache/gen
  18. type _cache interface {
  19. Info(c context.Context, key int64) (*v1.Info, error)
  20. //cache: -batch=50 -max_group=10 -batch_err=continue
  21. Infos(c context.Context, keys []int64) (map[int64]*v1.Info, error)
  22. Card(c context.Context, key int64) (*v1.Card, error)
  23. //cache: -batch=50 -max_group=10 -batch_err=continue
  24. Cards(c context.Context, keys []int64) (map[int64]*v1.Card, error)
  25. Vip(c context.Context, key int64) (*v1.VipInfo, error)
  26. //cache: -batch=50 -max_group=10 -batch_err=continue
  27. Vips(c context.Context, keys []int64) (map[int64]*v1.VipInfo, error)
  28. Profile(c context.Context, key int64) (*v1.Profile, error)
  29. }
  30. const (
  31. _nameURL = "/api/member/getInfoByName"
  32. _vipInfoURL = "/internal/v1/user/%d"
  33. _vipMultiInfoURL = "/internal/v1/user/list"
  34. _passportDetailURL = "/intranet/acc/detail"
  35. _passportProfile = "/intranet/acc/queryByMid"
  36. )
  37. // Dao dao.
  38. type Dao struct {
  39. // memcache
  40. mc *memcache.Pool
  41. mcExpire int32
  42. // cache async save
  43. cache *fanout.Fanout
  44. // rpc
  45. mRPC *member.Service
  46. suitRPC *usersuit.Service2
  47. // http
  48. httpR *bm.Client
  49. httpW *bm.Client
  50. httpP *bm.Client
  51. // api
  52. detailURI string
  53. profileURI string
  54. nameURI string
  55. // vip api
  56. vipInfoURI string
  57. vipMultiInfoURI string
  58. //es
  59. es *elastic.Elastic
  60. }
  61. // New new a dao.
  62. func New(c *conf.Config) (d *Dao) {
  63. d = &Dao{
  64. // account memcache
  65. mc: memcache.NewPool(c.Memcache.Account),
  66. mcExpire: int32(time.Duration(c.Memcache.AccountExpire) / time.Second),
  67. // cache chan
  68. cache: fanout.New("accountServiceCache", fanout.Worker(1), fanout.Buffer(1024)),
  69. // rpc
  70. // mRPC: member.New(c.MemberRPC),
  71. mRPC: member.New(c.MemberRPC),
  72. suitRPC: usersuit.New(c.SuitRPC),
  73. // http read&write client
  74. httpR: bm.NewClient(c.HTTPClient.Read),
  75. httpW: bm.NewClient(c.HTTPClient.Write),
  76. httpP: bm.NewClient(c.HTTPClient.Privacy),
  77. es: elastic.NewElastic(c.Elastic),
  78. // api
  79. nameURI: c.Host.AccountURI + _nameURL,
  80. // vip api
  81. vipInfoURI: c.Host.VipURI + _vipInfoURL,
  82. vipMultiInfoURI: c.Host.VipURI + _vipMultiInfoURL,
  83. //passport
  84. detailURI: c.Host.PassportURI + _passportDetailURL,
  85. profileURI: c.Host.PassportURI + _passportProfile,
  86. }
  87. return
  88. }
  89. // LevelExp get member level exp.
  90. func (d *Dao) LevelExp(c context.Context, mid int64) (lexp *mmodel.LevelInfo, err error) {
  91. lexp, err = d.mRPC.Exp(c, &mmodel.ArgMid2{Mid: mid})
  92. return
  93. }
  94. // AddMoral add moral.
  95. func (d *Dao) AddMoral(c context.Context, arg *mmodel.ArgUpdateMoral) (err error) {
  96. return d.mRPC.AddMoral(c, arg)
  97. }
  98. // UpdateExp update exp.
  99. func (d *Dao) UpdateExp(c context.Context, arg *mmodel.ArgAddExp) error {
  100. return d.mRPC.UpdateExp(c, arg)
  101. }
  102. // Ping check connection success.
  103. func (d *Dao) Ping(c context.Context) (err error) {
  104. conn := d.mc.Get(c)
  105. err = conn.Set(&memcache.Item{
  106. Key: "ping",
  107. Value: []byte("pong"),
  108. })
  109. conn.Close()
  110. return
  111. }
  112. // Close close memcache resource.
  113. func (d *Dao) Close() {
  114. if d.mc != nil {
  115. d.mc.Close()
  116. }
  117. }
  118. func fullImage(mid int64, image string) string {
  119. if len(image) == 0 {
  120. return ""
  121. }
  122. if strings.HasPrefix(image, "http://") {
  123. return image
  124. }
  125. return fmt.Sprintf("http://i%d.hdslb.com%s", mid%3, image)
  126. }