Created
June 14, 2021 21:51
-
-
Save ppazos/8205983f87fb4e4ab68766bba48fc9b6 to your computer and use it in GitHub Desktop.
buffer pattern sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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