1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
- namespace App\Command;
- use App\Model\BlackWord;
- use App\Model\WhiteWord;
- use Hyperf\Command\Annotation\Command;
- use Hyperf\Command\Command as HyperfCommand;
- use Hyperf\Di\Annotation\Inject;
- use Hyperf\Redis\Redis;
- // 假设这是一个命令类,用于执行导入违禁词的操作
- // 可以通过命令行运行该命令:php bin/hyperf.php import:whitewords
- #[Command]
- class ImportWhiteWordsCommand extends HyperfCommand
- {
- public function __construct()
- {
- parent::__construct('import:whitewords');
- }
- #[Inject]
- protected Redis $redis;
- public function handle()
- {
- // 假设这是从数据库或文件中获取的 30 万条违禁词数组
- // 这里只是示例,实际应用中需要从相应数据源读取
- $badWords = WhiteWord::pluck('name')->toArray();
- // 使用集合(Set)类型存储违禁词,键名为 'bad_words_set'
- $redisKey = 'white_word';
- $result = $this->redis->del($redisKey);
- if ($result === 1) {
- $this->info('已成功清空 white_word 集合。');
- } else {
- $this->error('清空 white_word 集合失败。');
- }
- // 开始事务,确保一次性导入所有违禁词
- $this->redis->multi();
- foreach ($badWords as $badWord) {
- $this->redis->sAdd($redisKey, $badWord);
- }
- $this->redis->exec();
- $this->info('白名单已成功导入到 Redis 中!');
- }
- }
|