Laravel, a popular PHP framework, which has been offering developers powerful tools to optimize web apps for decades. One such tool that has been gaining attention in 2024 is the Response Cache package. This package provides an efficient way to cache entire responses, significantly improving application performance. You can watch video here as well and follow the code example shown below:
So, I was saying, Response Cache is designed to work seamlessly with Laravel apps. It gives rooms to the developers to cache the complete response of a request, including headers and content. This approach has been particularly useful for pages that do not change frequently but receive high traffic.
Implementation of the Response Cache package is simply astoundingly straightforward. Developers are adding the package to their Laravel projects using Composer, the PHP dependency manager. Once installed, the package has been configurable through a simple configuration file, where the web app owners and devs are able to set caching duration, cache key generation methods, and other options. Here are some code examples demonstrating the usage of the Response Cache package in Laravel:
(i) Set Up
First, Use composer to install the package
composer require spatie/laravel-responsecache
Now we would make live the config file
php artisan vendor:publish --provider="Spatie\ResponseCache\ResponseCacheServiceProvider"
(ii) Use-case
To cache a response, you can use the ResponseCache middleware
use Spatie\ResponseCache\Middlewares\ResponseCacheMiddleware;
Route::get('/laramatic', function () {
return view('laramatic.index');
})->middleware(ResponseCacheMiddleware::class);
We could also use the ResponseCache facade to manually cache responses
use Spatie\ResponseCache\Facades\ResponseCache;
public function index()
{
if (ResponseCache::shouldCache()) {
$content = View::make('laramatic.index')->render();
ResponseCache::store($content);
return $content;
}
return view('laramatic.index');
}
(iii) Clear Cache
To clear the entire response cache:
use Spatie\ResponseCache\Facades\ResponseCache;
ResponseCache::clear();
clearing cachec for a typical URL
ResponseCache::forget('/laramatic');
(iv) Tags
You can tag cached responses and clear them selectively:
use Spatie\ResponseCache\Middlewares\ResponseCacheMiddleware;
Route::get('/laramatic', function () {
return view('laramatic.index');
})->middleware(ResponseCacheMiddleware::class)
->withTags(['laramatic', 'posts']);
// Later, to clear cache for all laramatic-related pages:
ResponseCache::clearByTag('laramatic');
(v) Excluding routes from caching
In your responsecache.php config file
'dont_cache' => [
'/admin/*',
'/user/profile',
],
(vi) Custom cache time
You can set custom cache times for specific routes
use Spatie\ResponseCache\Middlewares\ResponseCacheMiddleware;
Route::get('/weather', function () {
return view('weather.forecast');
})->middleware(ResponseCacheMiddleware::class . ':30'); // Cache for 30 minutes
One of the key features of the Response Cache package has been its ability to automatically clear the cache when the underlying data changes. This has been achieved through the use of tags and events. Many people online tag cached responses with model names, and when those models are updated or deleted, the corresponding cached responses are automatically invalidated at the end of the day.
Taylor Otwell, the creator of Laravel, has commented on the effectiveness of response caching: “The Response Cache package has been a game-changer for many Laravel applications. It has been providing a simple yet powerful way to dramatically improve performance without requiring extensive code changes.”
The package, then, would go on to offer us amazing flexibility in terms of cache drivers. While, to achieve that, Redis is a popular choice due to its speed and ability to handle high concurrency, developers are also been using other cache backends supported by Laravel, such as Memcached or even file-based caching for smaller applications. Apart from that, the security considerations have not been overlooked in the Response Cache package. It provides options to exclude sensitive routes from caching, ensuring that personalized or user-specific content is not inadvertently served from the cache to the wrong user.
The experts find this thing particularly useful for content-heavy-laden websites/apps, API endpoints with infrequently changing data, and high-traffic pages that do not require real-time updates. E-commerce product listings, blog post pages, and public API responses have been common use cases where the package has been employed effectively.












