1. Go to this page and download the library: Download alihaiderx/laravel-spool library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alihaiderx / laravel-spool example snippets
use Alihaiderx\LaravelSpool\Facades\Buffer;
// Call this on every page view, event, API hit, etc.
Buffer::buffer([
'payload' => [
'user_id' => $user->id,
'event' => 'page_view',
'url' => request()->path(),
]
], 'page-views');
use Alihaiderx\LaravelSpool\Facades\FileSystemBuffer;
Schedule::call(function () {
FileSystemBuffer::flush(function (string $file, ?string $bucket): bool {
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$rows = array_map(fn ($line) => unserialize($line), $lines);
// One DB insert for potentially thousands of events
DB::table('page_views')->insert($rows);
return true;
}, 'page-views');
})->everyMinute();
Schedule::call(function () {
FileSystemBuffer::clean(50, 'page-views');
})->daily();