Queueing
Recording a view is one INSERT into the views table. That is fast, but it still happens while the visitor waits for
the page. On a busy page, or with a database under load, you can hand the insert to a
queue worker instead, so the response goes out straight away.
How it works
Section titled “How it works”sequenceDiagram
participant Browser
participant App as Your app (request)
participant Queue
participant Worker
participant DB as views table
Browser->>App: GET /posts/my-post
App->>App: run the guards (bots, cooldown, ...)
App->>Queue: push RecordViewJob
App-->>Browser: the page
Queue->>Worker: RecordViewJob
Worker->>DB: INSERT the view
Everything that needs the request still happens during it: the guards decide whether the view counts, the cooldown is started, and the visitor, viewer, context and dimensions are captured, along with the time of the view. Only the insert waits for the worker. So a bot or a view on cooldown is never queued, and a view that lands a minute late still carries the time it happened.
1. Make sure a worker runs
Section titled “1. Make sure a worker runs”Queueing needs a queue connection that a worker processes, such as database or redis. If you haven’t set one up,
follow Laravel’s queue guide first, then start a worker:
php artisan queue:workOn the sync connection, Laravel’s default in a new app, a job runs during the request anyway, so you gain nothing.
php artisan views:doctor warns you when that is the case.
2. Queue every view
Section titled “2. Queue every view”Turn queueing on in config/eloquent-viewable.php:
'recording' => [ 'queue' => [ 'enabled' => true, 'connection' => null, // null uses the default queue connection 'queue' => null, // null uses the connection's default queue ],],To keep views apart from your other jobs, give them a queue of their own, 'queue' => 'views', and start a worker
for it with php artisan queue:work --queue=views.
Or queue only some views
Section titled “Or queue only some views”Leave the config off and choose per call:
views($post)->queue()->record(); // queued, even with queueing offviews($post)->queue(false)->record(); // stored now, even with queueing onThe middleware and the browser beacon take it as an argument:
Route::get('/posts/{post}', ShowPost::class) ->middleware(RecordViews::using('post', queue: true));What record() returns
Section titled “What record() returns”record() returns true as soon as the view is queued, before the worker has stored it. To tell the two apart, use
attempt():
$result = views($post)->queue()->attempt();
$result->recorded; // true$result->queued; // true, the worker stores itA count read right after a queued view may not include it yet.
In the worker
Section titled “In the worker”A job that fails is retried and ends up in failed_jobs like any other, following your worker’s --tries setting.
Queueing or the Redis buffer?
Section titled “Queueing or the Redis buffer?”Both take the insert out of the request. Queueing still inserts one row per view, so the database does the same amount of work, just later. The Redis buffer collects views in Redis and lands them in batches, which also takes the load off the database. Use one or the other: with the buffer on, leave queueing off.