编程语言
首页 > 编程语言> > php-将生成器分解为大块的最佳方法

php-将生成器分解为大块的最佳方法

作者:互联网

您能帮我写这段代码,将生成器的产量分成100个大块,并保存到数据库中吗?

$batchSize = 100;

$batch = [];
$i = 0;

/** 
 * @yield array $item
 */
foreach(itemsGenerator() as $item) {
    $batch[] = $item;
    $i++;

    if ($i === $batchSize) {
        Db::table('items')->save($batch);

        $batch = [];
        $i = 0;
    }

    $cnt++;
}

if ($batch) {
     Db::table('items')->save($batch);
}

我不想将逻辑分解成itemsGenerator

解决方法:

您可以将块逻辑放入单独的可重用函数中.

解决方案1:每个块都是一个生成器.

https://3v4l.org/3eSQm

function chunk(\Generator $generator, $n) {
    for ($i = 0; $generator->valid() && $i < $n; $generator->next(), ++$i) {
        yield $generator->current();
    }
}

function foo() {
  for ($i = 0; $i < 11; ++$i) {
    yield $i;
  }
}

for ($gen = foo(); $gen->valid();) {
    $chunk = [];
    foreach (chunk($gen, 3) as $value) {
        $chunk[] = $value;
    }
    print json_encode($chunk) . "\n";
}

解决方案2:每个块都是一个数组.

https://3v4l.org/aSfeR

function generator_chunks(\Generator $generator, $max_chunk_size) {
  $chunk = [];
  foreach ($generator as $item) {
    $chunk[] = $item;
    // @todo A local variable might be faster than count(), but adds clutter to the code. So using count() for this example code.
    if (count($chunk) >= $max_chunk_size) {
      yield $chunk;
      $chunk = [];
    }
  }
  if ([] !== $chunk) {
      // Remaining chunk with fewer items.
      yield $chunk;
  }
}

function generator() {
    for ($i = 0; $i < 11; ++$i) {
        yield $i;
    }
}

foreach (generator_chunks(generator(), 3) as $chunk) {
    print json_encode($chunk) . "\n";
}

现在,所有一个块都将作为数组一次存储在内存中,而不是整个序列.

可能有一些方法可以使每个块的行为都像一个生成器.但这又是另一回事了.

标签:chunks,foreach,generator,php
来源: https://codeday.me/bug/20191027/1946316.html