1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace app\admin\command;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\Request;
- use app\common\library\Ssdb;
- use app\common\constants\User;
- use think\Log;
- class SsdbIgnoreUserIdKeyFormat extends Command
- {
- /**
- * @var Output
- */
- private $_output;
- protected function configure()
- {
- $this->setName('SsdbIgnoreUserIdKeyFormat')
- ->setDescription('将原有白名单ignore-user转为IU,从分组0拆分一个大hash,到分组1为多个小hash');
- }
- protected function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin');
- $this->_output = $output;
- try {
- $this->logInfo('用户id白名单转为IU--begin');
- $ssdb = Ssdb::instance();
- $batchIndex = 0;
- $oldKey = 'ignore-user';
- while (true) {
- $batchIndex++;
- $valueList = $ssdb->hscan($oldKey, '', '', 5000);
- $this->logInfo(sprintf('批次:%s,key数量:%s。', $batchIndex, count($valueList)));
- if (empty($valueList)) {
- $this->logInfo('当前批次key数量为0,退出程序');
- break;
- }
- foreach ($valueList as $userId => $ip) {
- $this->logInfo(sprintf('%s userId:%s,Ip:%s,', date('Y-m-d H:i:s'), $userId, $ip));
- $ignoreUserIdKey = User::getIgnoreUserIdKey($userId);
- if (!$ssdb->hexists($ignoreUserIdKey, $userId)) {
- $ssdb->hset($ignoreUserIdKey, $userId, $ip);
- }
- $ssdb->hdel($oldKey, $userId);
- }
- }
- $this->logInfo('用户id白名单转为IU--end');
- } catch (\Exception $e) {
- $this->logError(date('Y-m-d H:i:s') . $e->getMessage());
- }
- }
- /**
- * @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);
- }
- }
|