12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace App\Command;
- use App\Model\BlackWord;
- use Hyperf\Command\Annotation\Command;
- use Hyperf\Command\Command as HyperfCommand;
- use Hyperf\Di\Annotation\Inject;
- use Hyperf\Redis\Redis;
- class ImportBadWordsCommand extends HyperfCommand
- {
- public function __construct()
- {
- parent::__construct('import:badwords');
- }
-
- protected Redis $redis;
- public function handle()
- {
-
-
- $badWords = BlackWord::pluck('name')->toArray();
-
- $redisKey = 'black_word';
- $result = $this->redis->del($redisKey);
- if ($result === 1) {
- $this->info('已成功清空 black_word 集合。');
- } else {
- $this->error('清空 black_word 集合失败。');
- }
-
- $this->redis->multi();
- foreach ($badWords as $badWord) {
- $this->redis->sAdd($redisKey, $badWord);
- }
- $this->redis->exec();
- $this->info('违禁词已成功导入到 Redis 中!');
- }
- }
|