123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- /**
- * Created by PhpStorm.
- * User: lts
- * Date: 13/8/18
- * Time: 下午3:34
- */
- namespace app\admin\command;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Option;
- use think\console\Output;
- use think\Request;
- use app\common\library\Ssdb;
- use app\common\constants\User;
- use think\Log;
- class SsdbIgnoreKeyFormat extends Command
- {
- /**
- * @var Output
- */
- private $_output;
- protected function configure()
- {
- $this->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);
- }
- }
|