php
34 lines · 5 steps
How a Laravel command warms report caches
An Artisan command precomputes expensive report pages per region so users never trigger a cold query.
Explained by
highlit
1namespace App\Console\Commands;
2
3use App\Models\Region;
4use App\Services\ReportBuilder;
5use Illuminate\Console\Command;
6use Illuminate\Support\Facades\Cache;
7
8class RefreshReportCache extends Command
9{
10 protected $signature = 'cache:refresh {--period=monthly}';
11
12 protected $description = 'Warm cached report pages so users never hit a cold, slow query';
13
14 public function handle(ReportBuilder $builder): int
15 {
16 $period = $this->option('period');
17 $regions = Region::active()->get();
18
19 $this->withProgressBar($regions, function (Region $region) use ($builder, $period) {
20 $key = "reports:{$period}:region:{$region->id}";
21
22 Cache::tags(['reports', "region:{$region->id}"])->put(
23 $key,
24 $builder->forRegion($region)->period($period)->generate(),
25 now()->addHours(6),
26 );
27 });
28
29 $this->newLine();
30 $this->info("Warmed {$regions->count()} {$period} report(s).");
31
32 return self::SUCCESS;
33 }
34}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Warming caches on a schedule shifts slow work off the request path and onto a background command.
- 2Cache tags let you group related entries so you can invalidate a whole set at once.
- 3Typehinting a service in handle lets Laravel's container inject it automatically.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-laravel-command-warms-report-caches-explained-php-59b6/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.