* @version : Beibao 1.0 */ namespace huo\controller\help; use huo\controller\common\Base; use huo\model\game\GameHelpModel; use think\Cache; class GameHelpCache extends Base { public static function ins() { return new static(); } /** * 获取QQ KEY * * @param string $game_id * * @return string */ private function getGmhKey($game_id) { return 'game_help_key_'.$game_id; } /** * 获取账户信息 * * @param string $game_id * * @return array|bool|mixed * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getInfoByGmhId($game_id) { $_key = $this->getGmhKey($game_id); $_gmh_data_json = Cache::get($_key); $_gmh_data = json_decode($_gmh_data_json, true); if (!is_array($_gmh_data)) { $_gmh_data = $_gmh_data_json; } if (!is_array($_gmh_data) || empty($_gmh_data)) { $_gmh_data = (new GameHelpModel())->getInfoByAppId($game_id); if (empty($_gmh_data)) { return false; } $this->saveGmhCache($game_id, $_gmh_data); } return $_gmh_data; } /** * 保存玩家cache 数据 * * @param $game_id * @param $gmh_data * @param int $ttl */ public function saveGmhCache($game_id, $gmh_data, $ttl = 3600) { $_key = $this->getGmhKey($game_id); Cache::set($_key, json_encode($gmh_data), $ttl); } /** * 更新帐号信息 * * @param string $game_id * @param array $gmh_data * * @return bool */ public function updateGmh($game_id, $gmh_data) { $_key = $this->getGmhKey($game_id); Cache::rm($_key); return (new GameHelpModel())->updateGmh($gmh_data, $game_id); } }