* @version : HUOSDK 8.0 */ namespace huo\controller\game; use huo\controller\common\Base; use huo\logic\game\GamePaySwitchLogic; use think\Cache; class GamePaySwitchCache extends Base { public static function ins() { return new static(); } /** * 获取KEY * * @param string $app_id * * @return string */ private function getPaySwitchKey($app_id) { return 'switch_id_key_'.$app_id; } /** * 获取信息 * * @param string $app_id * * @return array|bool|mixed */ public function getInfoByAppId($app_id) { $_key = $this->getPaySwitchKey($app_id); $_data_json = Cache::get($_key); $_data = json_decode($_data_json, true); if (!is_array($_data)) { $_data = $_data_json; } if (!is_array($_data) || empty($_data)) { $_data = (new GamePaySwitchLogic())->getInfoByAppId($app_id); if (empty($_data)) { return false; } $this->savePaySwitchCache($app_id, $_data); } return $_data; } /** * 保存cache 数据 * * @param $app_id * @param $data * @param int $ttl */ public function savePaySwitchCache($app_id, $data, $ttl = 3600) { $_key = $this->getPaySwitchKey($app_id); Cache::set($_key, json_encode($data), $ttl); } /** * 更新信息 * * @param string $app_id * @param array $data * * @param bool $rm_cache 是否删除缓存 * * @return \huo\model\game\GamePaySwitchModel */ public function updatePaySwitch($app_id, $data, $rm_cache = false) { if (true == $rm_cache) { $_key = $this->getPaySwitchKey($app_id); Cache::rm($_key); } else { $_old_data = $this->getInfoByAppId($app_id); if (!empty($_old_data)) { $data = array_merge($_old_data, $data); } $this->savePaySwitchCache($app_id, $data); } return (new GamePaySwitchLogic())->updatePaySwitch($data, $app_id); } }