GiftCache.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * GiftCache.php UTF-8
  4. *
  5. *
  6. * @date : 2018/5/3 15:46
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\gift;
  13. use huo\controller\common\Base;
  14. use huo\logic\game\GiftLogic;
  15. use huo\model\game\GiftModel;
  16. use think\Cache;
  17. class GiftCache extends Base {
  18. public static function ins() {
  19. return new static();
  20. }
  21. /**
  22. * 获取礼包KEY
  23. *
  24. * @param string $gift_id
  25. *
  26. * @return string
  27. */
  28. private function getGiftKey($gift_id) {
  29. return 'gift_id_key_'.$gift_id;
  30. }
  31. /**
  32. * 获取礼包信息
  33. *
  34. * @param string $gift_id
  35. *
  36. * @return array|bool|mixed
  37. */
  38. public function getInfoByGiftId($gift_id) {
  39. $_key = $this->getGiftKey($gift_id);
  40. $_gift_data_json = Cache::get($_key);
  41. $_gift_data = json_decode($_gift_data_json, true);
  42. if (!is_array($_gift_data)) {
  43. $_gift_data = $_gift_data_json;
  44. }
  45. if (!is_array($_gift_data) || empty($_gift_data)) {
  46. $_gift_data = (new GiftLogic())->getInfoByGiftId($gift_id);
  47. if (empty($_gift_data)) {
  48. return false;
  49. }
  50. $this->saveGiftCache($gift_id, $_gift_data);
  51. }
  52. return $_gift_data;
  53. }
  54. /**
  55. * 保存礼包cache 数据
  56. *
  57. * @param $gift_id
  58. * @param $_gift_data
  59. * @param int $ttl
  60. */
  61. public function saveGiftCache($gift_id, $_gift_data, $ttl = 3600) {
  62. $_key = $this->getGiftKey($gift_id);
  63. Cache::set($_key, json_encode($_gift_data), $ttl);
  64. }
  65. /**
  66. * 更新礼包信息
  67. *
  68. * @param string $gift_id
  69. * @param array $gift_data
  70. *
  71. * @return bool
  72. * @throws \think\Exception
  73. */
  74. public function updateGift($gift_id, $gift_data) {
  75. $this->deleteGiftCache($gift_id);
  76. $_rs = (new GiftModel())->updateGift($gift_data, $gift_id);
  77. if (true == $_rs) {
  78. (new GiftLogic())->checkGameGift($gift_id);
  79. $this->updateGameGift($gift_id);
  80. }
  81. return $_rs;
  82. }
  83. /**
  84. * 删除礼包cache 数据
  85. *
  86. * @param string $gift_id
  87. *
  88. * @return void
  89. * @author chenbingling <cbl@huosdk.com>
  90. * @date 2018/5/9 14:15
  91. */
  92. public function deleteGiftCache($gift_id) {
  93. $_key = $this->getGiftKey($gift_id);
  94. Cache::rm($_key);
  95. }
  96. /**
  97. * 获取游戏礼包KEY
  98. *
  99. * @param string $game_id
  100. *
  101. * @return string
  102. */
  103. private function getGameGiftKey($game_id) {
  104. return 'game_gift_key_'.$game_id;
  105. }
  106. /**
  107. * 通过游戏ID获取礼包数量
  108. *
  109. * @param $game_id
  110. *
  111. * @return int
  112. */
  113. public function getGiftCntByGameId($game_id) {
  114. if (empty($game_id)) {
  115. return 0;
  116. }
  117. $_game_gift_data = $this->getGameGiftByGameId($game_id);
  118. return count($_game_gift_data['list']);
  119. }
  120. /**
  121. * 获取游戏礼包信息
  122. *
  123. * @param string $game_id
  124. *
  125. * @return array|bool|mixed
  126. */
  127. public function getGameGiftByGameId($game_id) {
  128. $_key = $this->getGameGiftKey($game_id);
  129. $_gg_data_json = Cache::get($_key);
  130. $_gg_data = json_decode($_gg_data_json, true);
  131. if (!is_array($_gg_data)) {
  132. $_gg_data = $_gg_data_json;
  133. }
  134. if (!is_array($_gg_data) || empty($_gg_data)) {
  135. $_gg_data = (new GiftLogic())->getGameGiftByGameId($game_id);
  136. if (is_numeric($_gg_data)) {
  137. return false;
  138. }
  139. $this->saveGameGiftCache($game_id, $_gg_data);
  140. }
  141. return $_gg_data;
  142. }
  143. /**
  144. * 保存礼包cache 数据
  145. *
  146. * @param string $game_id
  147. * @param array $game_gift_data
  148. * @param int $ttl
  149. */
  150. public function saveGameGiftCache($game_id, $game_gift_data, $ttl = 3600) {
  151. $_key = $this->getGameGiftKey($game_id);
  152. Cache::set($_key, json_encode($game_gift_data), $ttl);
  153. }
  154. /**
  155. * 更新礼包信息
  156. *
  157. * @param string $game_id
  158. */
  159. public function updateGameGift($game_id) {
  160. $_key = $this->getGameGiftKey($game_id);
  161. Cache::rm($_key);
  162. }
  163. }