setName('SsdbIgnoreKeyFormat') ->setDescription('将原有白名单ignore转为IG,从分组0拆分一个大hash,到分组1为多个小hash'); } protected function execute(Input $input, Output $output) { Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下 $this->_output = $output; try { $msg = '白名单ignore转为IG--begin'; $this->logInfo($msg); $ssdb = Ssdb::instance(); $oldKey = 'ignore'; $batchIndex = 0; while (true) { $batchIndex++; $ipList = $ssdb->hkeys($oldKey, '', '', 5000); $this->logInfo(sprintf('批次:%s,key数量:%s。', $batchIndex, count($ipList))); if (empty($ipList)) { $this->logInfo('当前批次key数量为0,退出程序'); break; } foreach ($ipList as $ip) { $msg = sprintf('%s IgnoreIp:%s', date('Y-m-d H:i:s'), $ip); $this->logInfo($msg); $ignoreIpKey = User::getIgnoreIpKey($ip); if (!$ssdb->hexists($ignoreIpKey, $ip)) { $ssdb->hset($ignoreIpKey, $ip, ''); } $ssdb->hdel($oldKey, $ip); } } $msg = '白名单ignore转为IG--end'; $this->logInfo($msg); } catch (\Exception $e) { $errMsg = date('Y-m-d H:i:s') . $e->getMessage(); $this->logError($errMsg); } } /** * @param $msg */ private function logInfo($msg) { Log::info($msg); $this->_output->writeln($msg); } /** * @param $msg */ private function logError($msg) { Log::error($msg); $this->_output->writeln($msg); } }