把340万个单词的csv文件写进redis队列
时间:2024-7-12 10:58 作者:xiang 分类: 无
<?php
declare(strict_types=1);
namespace App\Command;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\Context\ApplicationContext;
use League\Csv\Reader;
use Psr\Container\ContainerInterface;
#[Command]
class FooCommand extends HyperfCommand
{
public function __construct(protected ContainerInterface $container)
{
parent::__construct('demo:command');
}
public function configure()
{
parent::configure();
$this->setDescription('Hyperf Demo Command');
}
public function handle()
{
$stime = microtime(true);
$reader = Reader::createFromPath('/var/www/html/hyperf/hyperf-csv/stardict.csv', 'r');
$reader->setHeaderOffset(0);
$records = $reader->getRecords();
foreach ($records as $record) {
if (count($record) == 1 && $record[0] === '') {
return;
}
$word = preg_replace('/[^\w\s]/u', '', $record['word']);
$_insert['word'] = $word;
$_insert['phonetic'] = $record['phonetic'];
$_insert['definition'] = $record['definition'];
$_insert['translation'] = $record['translation'];
$key = "dict";
$value = json_encode($_insert);
$container = ApplicationContext::getContainer();
$redis = $container->get(\Hyperf\Redis\Redis::class);
$redis->lPush($key, $value);
print_r($word."\n");
}
$etime = microtime(true);
$total = $etime - $stime;
print_r($total);
}
}
耗时176秒,内存400多MB。