devlop/buffer

简单缓冲,用于迭代可迭代的对象并定期应用回调。

1.2.0 2021-04-01 04:43 UTC

This package is auto-updated.

Last update: 2024-09-29 06:02:50 UTC


README

Latest Stable Version License

缓冲

简单缓冲,用于迭代可迭代的对象并定期应用回调。

这允许您以最小的内存使用量消耗大量数组。

安装

composer require devlop/buffer

用法

手册

这种方式给您最大的权限,但也迫使您承担更多的执行责任。

use Devlop\Buffer\Buffer;

$bigFuckingArray = [...]; // array containing between zero and many many items

$buffer = new Buffer(
    10, // max Buffer size
    function (array $items) : void {
        // callback to apply when buffer size reaches max
    },
);

foreach ($bigFuckingArray as $key => $value) {
    $buffer->push($value); // the Buffer callback will automatically be applied when needed
}

// important, after looping over the array, remember to manually call the flush() method to apply the callback on last time if needed
$buffer->flush();

自动

这种方式是使用缓冲的最简单方式,并且需要您最少的工作。

use Devlop\Buffer\Buffer;

$bigFuckingArray = [...]; // array containing between zero and many many items

Buffer::iterate(
    $bigFuckingArray, // input iterable
    10, // max Buffer size
    function (array $items) : void {
        // callback to apply when buffer size reaches max
        // the callback will also be called one last time after finishing iterating if needed
    },
)