GameQqCache.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * GameQqCache.php UTF-8
  4. * WWW
  5. *
  6. * @date : 2018/7/13 20:42
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\game;
  13. use huo\controller\common\Base;
  14. use huo\logic\game\GameQqLogic;
  15. use huo\model\game\GameQqModel;
  16. use think\Cache;
  17. class GameQqCache extends Base {
  18. protected $tag = 'game_qq_tag';
  19. /**
  20. * 获取KEY
  21. *
  22. * @param string $app_id
  23. *
  24. *
  25. * @return string
  26. */
  27. private function getGameQqKey($app_id) {
  28. return 'game_qq_id_key_'.$app_id;
  29. }
  30. /**
  31. * 获取信息
  32. *
  33. * @param string $app_id
  34. *
  35. *
  36. * @return array|bool|mixed
  37. * @throws \think\Exception
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. * @throws \think\exception\DbException
  41. */
  42. public function getGameQqInfo($app_id) {
  43. $_key = $this->getGameQqKey($app_id);
  44. $_game_data_json = Cache::get($_key);
  45. $_game_data = json_decode($_game_data_json, true);
  46. if (!is_array($_game_data)) {
  47. $_game_data = $_game_data_json;
  48. }
  49. if (!is_array($_game_data) || empty($_game_data)) {
  50. $_game_data = (new GameQqLogic())->getGameQqInfo($app_id);
  51. if (empty($_game_data)) {
  52. return false;
  53. }
  54. $this->saveGameQqCache($app_id, $_game_data);
  55. }
  56. return $_game_data;
  57. }
  58. /**
  59. * 保存cache 数据
  60. *
  61. * @param $app_id
  62. * @param $_game_data
  63. * @param int $ttl
  64. */
  65. public function saveGameQqCache($app_id, $_game_data, $ttl = 3600) {
  66. $_key = $this->getGameQqKey($app_id);
  67. Cache::tag($this->tag)->set($_key, json_encode($_game_data), $ttl);
  68. }
  69. /**
  70. * 更新信息
  71. *
  72. * @param array $game_data
  73. *
  74. *
  75. * @return bool
  76. */
  77. public function updateGame($game_data) {
  78. Cache::clear($this->tag);
  79. return (new GameQqModel())->updateGame($game_data, $game_data['id']);
  80. }
  81. /**
  82. * 添加信息
  83. *
  84. * @param $game_data
  85. *
  86. * @return bool|mixed
  87. */
  88. public function addGame($game_data) {
  89. Cache::clear($this->tag);
  90. return (new GameQqModel())->addGame($game_data);
  91. }
  92. }