Kritim Yantra
Jun 25, 2025
“Why make 100 database calls when one can be enough?”
When your API starts growing, every request adds load on your database, slows down the response, and drains your server resources. The solution? Caching.
In this blog, you’ll learn how to boost the speed of your Laravel 12 API using caching strategies — step by step.
Caching is like storing your homework in your backpack instead of rewriting it every time a teacher asks.
Instead of querying the database every time someone hits your API, you store the result temporarily — so it loads in milliseconds.
Laravel supports various cache drivers:
file
(default)redis
database
memcached
array
(for testing)In your .env
file, set the driver:
CACHE_DRIVER=file
To boost performance in production, use Redis:
CACHE_DRIVER=redis
Install Redis via:
composer require predis/predis
Let’s say you have a PostController@index
method:
public function index()
{
return Post::latest()->paginate(10);
}
use Illuminate\Support\Facades\Cache;
public function index()
{
$posts = Cache::remember('posts_page_1', 60, function () {
return Post::latest()->paginate(10);
});
return $posts;
}
✅ This caches the first page of posts for 60 minutes.
remember()
BreakdownCache::remember('key', ttl, function () {
// Fetch from DB if not cached
});
key
: Unique cache namettl
: Time to live (in seconds/minutes)Whenever a new post is created or updated, we should clear the cache.
In your store()
or update()
method:
Cache::forget('posts_page_1');
That way, new data is re-cached on the next request.
If you paginate results (page 1, 2, 3...), cache each page separately:
$page = request()->get('page', 1);
$posts = Cache::remember("posts_page_$page", 60, function () {
return Post::latest()->paginate(10);
});
This ensures page 1, 2, and so on are all individually cached.
When using Redis or Memcached, you can tag cache items:
Cache::tags(['posts'])->remember('recent_posts', 60, function () {
return Post::latest()->take(5)->get();
});
Then you can clear the tag when needed:
Cache::tags(['posts'])->flush();
This is powerful for large apps managing multiple cache segments.
Avoid caching personal or user-specific API responses unless you're keying them by user ID:
$userId = auth()->id();
$dashboard = Cache::remember("dashboard_user_$userId", 300, function () {
return [
'stats' => fetchUserStats($userId),
'notifications' => fetchAlerts($userId),
];
});
{
"cached": true,
"data": [
{
"id": 1,
"title": "Laravel Caching is Powerful"
}
]
}
You can even include "cached": true
in your response for debugging purposes.
Data Type | Expire After |
---|---|
Homepage posts | 1 hour |
User dashboard | 5-15 minutes |
Product catalog | 1 day |
Live stats | 30 seconds |
Use ttl
based on how often the data changes.
Cache::remember()
to store responsesCache::forget()
after create/updateFor heavy APIs, consider using middleware like:
composer require spatie/laravel-responsecache
This package caches entire API responses, not just data.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google