php
45 lines · 5 steps
Tagged cache invalidation in Laravel
A service class caches product and category reads under overlapping tags so related entries can be flushed together precisely.
Explained by
highlit
1<?php
2
3namespace App\Services;
4
5use App\Models\Product;
6use Illuminate\Support\Facades\Cache;
7
8class CatalogCache
9{
10 private const TTL = 3600;
11
12 public function forProduct(int $productId): Product
13 {
14 return Cache::tags(['catalog', "product:{$productId}"])->remember(
15 "product:{$productId}",
16 self::TTL,
17 fn () => Product::with('variants', 'category')->findOrFail($productId)
18 );
19 }
20
21 public function forCategory(int $categoryId): array
22 {
23 return Cache::tags(['catalog', "category:{$categoryId}"])->remember(
24 "category:{$categoryId}:products",
25 self::TTL,
26 fn () => Product::where('category_id', $categoryId)
27 ->orderBy('name')
28 ->get()
29 ->all()
30 );
31 }
32
33 public function flushProduct(Product $product): void
34 {
35 Cache::tags([
36 "product:{$product->id}",
37 "category:{$product->category_id}",
38 ])->flush();
39 }
40
41 public function flushAll(): void
42 {
43 Cache::tags('catalog')->flush();
44 }
45}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Tagging cache entries lets you invalidate groups of related keys without tracking each key by hand.
- 2Overlapping tags (a broad one plus specific ones) give you both fine-grained and sweeping invalidation from the same store.
- 3Wrapping expensive reads in remember keeps the caching concern in one place instead of scattered across callers.
Related explainers
python
import threading from functools import wraps
A thread-safe debounce decorator in Python
decorators
debounce
threading
Advanced
6 steps
go
package middleware import ( "log"
Per-IP rate limiting middleware in Gin
rate-limiting
middleware
concurrency
Intermediate
8 steps
php
final class BitmapGrid { private SplFixedArray $cells;
A flat-array bitmap grid in PHP
data-structures
row-major-order
bitmap
Intermediate
7 steps
javascript
const crypto = require('crypto'); function inMemoryCache({ maxAge = 60_000, maxEntries = 500 } = {}) { const store = new Map();
Building an in-memory cache middleware in Express
caching
middleware
etag
Advanced
10 steps
php
namespace App\Http\Filters; use Closure; use Illuminate\Database\Eloquent\Builder;
Composable query filters with Laravel's Pipeline
pipeline
query-builder
single-responsibility
Intermediate
7 steps
php
namespace App\Providers; use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
Adding a whereActive macro in Laravel
macros
service-provider
query-builder
Intermediate
5 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/tagged-cache-invalidation-in-laravel-explained-php-cf77/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.