Skip to content

Instantly share code, notes, and snippets.

@ppazos
Created June 14, 2021 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppazos/8205983f87fb4e4ab68766bba48fc9b6 to your computer and use it in GitHub Desktop.
Save ppazos/8205983f87fb4e4ab68766bba48fc9b6 to your computer and use it in GitHub Desktop.
buffer pattern sample
<?php
// these could be elements in a db loaded by small chunks into the buffer to avoid having too much memory consumption
$data = range(1, 1000);
$pointer = 0;
$buffer_size = 10; // process just these items per loop
$buffer = array();
do {
// load buffer
for ($i=0; $i<$buffer_size; $i++)
{
$buffer[$i] = $data[$i+$pointer];
}
if ($buffer)
{
// process buffer
print_r($buffer);
$pointer += $buffer_size;
}
} while (count($data) > $pointer); // do we have items to process?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment