GameHelpCache.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * GameHelpCache.php UTF-8
  4. * web
  5. *
  6. * @date : 2018/7/19 10:01
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : Beibao 1.0
  11. */
  12. namespace huo\controller\help;
  13. use huo\controller\common\Base;
  14. use huo\model\game\GameHelpModel;
  15. use think\Cache;
  16. class GameHelpCache extends Base {
  17. public static function ins() {
  18. return new static();
  19. }
  20. /**
  21. * 获取QQ KEY
  22. *
  23. * @param string $game_id
  24. *
  25. * @return string
  26. */
  27. private function getGmhKey($game_id) {
  28. return 'game_help_key_'.$game_id;
  29. }
  30. /**
  31. * 获取账户信息
  32. *
  33. * @param string $game_id
  34. *
  35. * @return array|bool|mixed
  36. * @throws \think\Exception
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @throws \think\exception\DbException
  40. */
  41. public function getInfoByGmhId($game_id) {
  42. $_key = $this->getGmhKey($game_id);
  43. $_gmh_data_json = Cache::get($_key);
  44. $_gmh_data = json_decode($_gmh_data_json, true);
  45. if (!is_array($_gmh_data)) {
  46. $_gmh_data = $_gmh_data_json;
  47. }
  48. if (!is_array($_gmh_data) || empty($_gmh_data)) {
  49. $_gmh_data = (new GameHelpModel())->getInfoByAppId($game_id);
  50. if (empty($_gmh_data)) {
  51. return false;
  52. }
  53. $this->saveGmhCache($game_id, $_gmh_data);
  54. }
  55. return $_gmh_data;
  56. }
  57. /**
  58. * 保存玩家cache 数据
  59. *
  60. * @param $game_id
  61. * @param $gmh_data
  62. * @param int $ttl
  63. */
  64. public function saveGmhCache($game_id, $gmh_data, $ttl = 3600) {
  65. $_key = $this->getGmhKey($game_id);
  66. Cache::set($_key, json_encode($gmh_data), $ttl);
  67. }
  68. /**
  69. * 更新帐号信息
  70. *
  71. * @param string $game_id
  72. * @param array $gmh_data
  73. *
  74. * @return bool
  75. */
  76. public function updateGmh($game_id, $gmh_data) {
  77. $_key = $this->getGmhKey($game_id);
  78. Cache::rm($_key);
  79. return (new GameHelpModel())->updateGmh($gmh_data, $game_id);
  80. }
  81. }