SsdbIgnoreUserIdKeyFormat.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace app\admin\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\Output;
  6. use think\Request;
  7. use app\common\library\Ssdb;
  8. use app\common\constants\User;
  9. use think\Log;
  10. class SsdbIgnoreUserIdKeyFormat extends Command
  11. {
  12. /**
  13. * @var Output
  14. */
  15. private $_output;
  16. protected function configure()
  17. {
  18. $this->setName('SsdbIgnoreUserIdKeyFormat')
  19. ->setDescription('将原有白名单ignore-user转为IU,从分组0拆分一个大hash,到分组1为多个小hash');
  20. }
  21. protected function execute(Input $input, Output $output)
  22. {
  23. Request::instance()->module('admin');
  24. $this->_output = $output;
  25. try {
  26. $this->logInfo('用户id白名单转为IU--begin');
  27. $ssdb = Ssdb::instance();
  28. $batchIndex = 0;
  29. $oldKey = 'ignore-user';
  30. while (true) {
  31. $batchIndex++;
  32. $valueList = $ssdb->hscan($oldKey, '', '', 5000);
  33. $this->logInfo(sprintf('批次:%s,key数量:%s。', $batchIndex, count($valueList)));
  34. if (empty($valueList)) {
  35. $this->logInfo('当前批次key数量为0,退出程序');
  36. break;
  37. }
  38. foreach ($valueList as $userId => $ip) {
  39. $this->logInfo(sprintf('%s userId:%s,Ip:%s,', date('Y-m-d H:i:s'), $userId, $ip));
  40. $ignoreUserIdKey = User::getIgnoreUserIdKey($userId);
  41. if (!$ssdb->hexists($ignoreUserIdKey, $userId)) {
  42. $ssdb->hset($ignoreUserIdKey, $userId, $ip);
  43. }
  44. $ssdb->hdel($oldKey, $userId);
  45. }
  46. }
  47. $this->logInfo('用户id白名单转为IU--end');
  48. } catch (\Exception $e) {
  49. $this->logError(date('Y-m-d H:i:s') . $e->getMessage());
  50. }
  51. }
  52. /**
  53. * @param $msg
  54. */
  55. private function logInfo($msg)
  56. {
  57. Log::info($msg);
  58. $this->_output->writeln($msg);
  59. }
  60. /**
  61. * @param $msg
  62. */
  63. private function logError($msg)
  64. {
  65. Log::error($msg);
  66. $this->_output->writeln($msg);
  67. }
  68. }