* @version : HUOSDK 8.0 */ namespace huo\controller\game; use huo\controller\common\Base; use huo\logic\game\GamePayShowLogic; use huolib\constant\CacheConst; use think\Cache; class GamePayShowCache extends Base { public static function ins() { return new static(); } /** * 获取KEY * * @param string $app_id * * @return string */ private function getPayShowKey($app_id) { return CacheConst::CACHE_GAME_PAY_SHOW_PREFIX.$app_id; } /** * 获取信息 * * @param string $app_id * * @return array|bool|mixed */ public function getInfoByAppId($app_id) { $_key = $this->getPayShowKey($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 GamePayShowLogic())->getInfoByAppId($app_id); if (empty($_data)) { return false; } $this->savePayShowCache($app_id, $_data); } return $_data; } /** * 保存cache 数据 * * @param $app_id * @param $data * @param int $ttl */ public function savePayShowCache($app_id, $data, $ttl = 3600) { $_key = $this->getPayShowKey($app_id); Cache::set($_key, json_encode($data), $ttl); } /** * 更新信息 * * @param string $app_id * @param array $data * * @param bool $rm_cache 是否删除缓存 * * @return \huo\model\game\GamePayShowModel */ public function updatePayShow($app_id, $data, $rm_cache = false) { if (true == $rm_cache) { $_key = $this->getPayShowKey($app_id); Cache::rm($_key); } else { $_old_data = $this->getInfoByAppId($app_id); if (!empty($_old_data)) { $data = array_merge($_old_data, $data); } $this->savePayShowCache($app_id, $data); } return (new GamePayShowLogic())->updatePaySwitch($data, $app_id); } }