PHP code example of alihaiderx / laravel-spool

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();

return [
    'buffer_driver'    => env('SPOOL_BUFFER_DRIVER', 'file'),
    'max_shards'       => env('SPOOL_MAX_SHARDS', 30),
    'max_shard_size'   => env('SPOOL_MAX_SHARD_SIZE', 307200),
    'max_flush_shards' => env('SPOOL_MAX_FLUSH_SHARDS', 5),
    'shards_ttl_days'  => env('SPOOL_SHARDS_TTL_DAYS', 3),
    'redis_batch_size' => env('SPOOL_REDIS_BATCH_SIZE', 500),
];

use Alihaiderx\LaravelSpool\Facades\Buffer;
use Alihaiderx\LaravelSpool\Events\RedisBufferConsumeEvent;

Buffer::listenRedis(function (RedisBufferConsumeEvent $event) {
    $pageViews = array_filter(
        $event->batch,
        fn ($item) => $item['bucketSlug'] === 'page-views'
    );

    DB::table('page_views')->insert(array_column($pageViews, 'payload'));
});
bash
php artisan spool:install
bash
php artisan spool:start-redis-consume
bash
php artisan spool:health

ERROR  Buffer 'active' directory does not exist: .../storage/app/private/buffer/active
ERROR  max_flush_shards (10) cannot exceed max_shards (5).
bash
php artisan spool:start-redis-consume