Cache Invalidation: Keeping Cached Data Correct
Learn how to invalidate cache safely when data changes, using patterns you can trust in production.
Jonathan Reed
November 17, 2025
3.8k144
Caching speeds up your app, but stale cache can show wrong data. A professional caching strategy always answers one question:
When does cached data become invalid?
## Pattern 1: Time-based expiration
```php
Cache::remember('top_posts', 60, fn () => Post::top()->get());
```
Simple, but not always accurate.
## Pattern 2: Forget cache when data changes
```php
Post::created(function () {
Cache::forget('top_posts');
});
```
## Pattern 3: Versioned cache keys
```php
$key = 'top_posts:v' . Cache::get('top_posts_version', 1);
```
When posts update, bump the version.
## Graph: Invalidation flow
```mermaid
flowchart TD
A[Data Updated] --> B[Invalidate Cache]
B --> C[Next Request Rebuilds Cache]
```
In the next tutorial, we will implement file downloads and streaming large exports.
#Laravel#Caching#Performance#Advanced