game.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. "go-common/app/job/openplatform/article/model"
  8. "go-common/library/cache/redis"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _gameSyncURL = "http://line3-h5-mobile-api.biligame.com/h5/internal/article/sync"
  14. _typeAdd = "1"
  15. _typeUpdate = "2"
  16. _typeDel = "3"
  17. _gameKey = "artjob_game_mids"
  18. )
  19. // GameSync game sync
  20. func (d *Dao) GameSync(c context.Context, action string, cvid int64) (err error) {
  21. params := url.Values{}
  22. params.Set("timestamp", strconv.FormatInt(time.Now().Unix()*1000, 10))
  23. var tp string
  24. if action == model.ActInsert {
  25. tp = _typeAdd
  26. } else if action == model.ActUpdate {
  27. tp = _typeUpdate
  28. } else {
  29. tp = _typeDel
  30. }
  31. params.Set("type", tp)
  32. params.Set("article_id", strconv.FormatInt(cvid, 10))
  33. resp := struct {
  34. Code int
  35. }{}
  36. if err = d.gameHTTPClient.Post(c, _gameSyncURL, "", params, &resp); err != nil {
  37. log.Error("game: d.gameHTTPClient.Post(%s) error(%+v)", _gameSyncURL+params.Encode(), err)
  38. PromError("game:同步数据")
  39. return
  40. }
  41. if resp.Code != 0 {
  42. err = ecode.Int(resp.Code)
  43. log.Error("game: d.gameHTTPClient.Get(%s) code: %v error(%+v)", _gameSyncURL, resp.Code, err)
  44. PromError("game:同步数据")
  45. return
  46. }
  47. log.Info("game: dao.GameSync success action: %v cvid: %v", action, cvid)
  48. return
  49. }
  50. // CacheGameList .
  51. func (d *Dao) CacheGameList(c context.Context) (mids []int64, err error) {
  52. conn := d.redis.Get(c)
  53. defer conn.Close()
  54. if mids, err = redis.Int64s(conn.Do("ZRANGE", _gameKey, 0, -1)); err != nil {
  55. PromError("redis:游戏列表缓存")
  56. log.Error("conn.Zrange(%s) error(%+v)", _gameKey, err)
  57. }
  58. return
  59. }
  60. // AddCacheGameList .
  61. func (d *Dao) AddCacheGameList(c context.Context, mids []int64) (err error) {
  62. var (
  63. key = _gameKey
  64. conn = d.redis.Get(c)
  65. count int
  66. )
  67. defer conn.Close()
  68. if len(mids) == 0 {
  69. return
  70. }
  71. if err = conn.Send("DEL", key); err != nil {
  72. PromError("redis:删除游戏列表缓存")
  73. log.Error("conn.Send(DEL, %s) error(%+v)", key, err)
  74. return
  75. }
  76. count++
  77. for i, mid := range mids {
  78. score := i
  79. if err = conn.Send("ZADD", key, "CH", score, mid); err != nil {
  80. PromError("redis:增加游戏列表缓存")
  81. log.Error("conn.Send(ZADD, %s, %d, %v) error(%+v)", key, score, mid, err)
  82. return
  83. }
  84. count++
  85. }
  86. if err = conn.Send("EXPIRE", key, d.gameCacheExpire); err != nil {
  87. PromError("redis:游戏列表缓存设定过期")
  88. log.Error("conn.Send(EXPIRE, %s, %d) error(%+v)", key, d.gameCacheExpire, err)
  89. return
  90. }
  91. count++
  92. if err = conn.Flush(); err != nil {
  93. PromError("redis:增加游戏列表缓存flush")
  94. log.Error("conn.Flush error(%+v)", err)
  95. return
  96. }
  97. for i := 0; i < count; i++ {
  98. if _, err = conn.Receive(); err != nil {
  99. PromError("redis:增加游戏列表缓存receive")
  100. log.Error("conn.Receive error(%+v)", err)
  101. return
  102. }
  103. }
  104. return
  105. }