The Spatie Response Cache package, in 2024, has become increasingly popular among developers who use Laravel on a regular basis for a number of web apps. This useful tool has been helping to make web apps powered by Laravel run faster by saving entire web responses. So let’s learn about spatie response cache package mentioned with featured code examples and also a video to further your imagination. Laravel, which is a widely-used framework for building websites, it’s been used for like decades, has always been known for its good features. The Response Cache package, on the other hand, created by a company called Spatie, to be honest, keeps making Laravel-powered-web-apps even better by allowing developers to save complete web responses, including all the information that goes with them.
Setting up the Response Cache package has been fairly simple. Developers use it to their Laravel projects using a tool called Composer. After installation, they are able to set it up easily by changing a few settings in a file. These settings include how long to save responses and how to create unique names for each saved response. You could see the code examples below for better understanding
We would set up the package first
First, developers have been installing the package using Composer
composer require spatie/laravel-responsecache
Now, we would publish the configuration file
php artisan vendor:publish --provider="Spatie\ResponseCache\ResponseCacheServiceProvider"
Basic Use-case
To cache a response, developers mostly use the ResponseCache middleware
use Spatie\ResponseCache\Middlewares\ResponseCacheMiddleware;
Route::get('/blog', function () {
return view('blog.index');
})->middleware(ResponseCacheMiddleware::class);
Here is how we would use ResponseCache facade to manually cache responses
use Spatie\ResponseCache\Facades\ResponseCache;
public function index()
{
if (ResponseCache::shouldCache()) {
$content = View::make('blog.index')->render();
ResponseCache::store($content);
return $content;
}
return view('blog.index');
}
Cache ClearingÂ
To clear the entire response cache, developers mostly use
use Spatie\ResponseCache\Facades\ResponseCache;
ResponseCache::clear();
To clear the cache for a specific URL, we could use
ResponseCache::forget('/blog');
Using tags
While we could also tag cached responses and clearing them selectively using tags see the code snippet below for that
use Spatie\ResponseCache\Middlewares\ResponseCacheMiddleware;
Route::get('/blog', function () {
return view('blog.index');
})->middleware(ResponseCacheMiddleware::class)
->withTags(['blog', 'posts']);
// Later, to clear cache for all blog-related pages:
ResponseCache::clearByTag('blog');
We can exclude routes from getting cachedÂ
In the responsecache.php config file, we could specify
'dont_cache' => [
'/admin/*',
'/user/profile',
],
Here is How to Go About Customizing Caching Time
We would custom cache times for specific routes see the code example below:
use Spatie\ResponseCache\Middlewares\ResponseCacheMiddleware;
Route::get('/weather', function () {
return view('weather.forecast');
})->middleware(ResponseCacheMiddleware::class . ':20'); // Cache for 20 minutes
You have seen the above code examples for different events. One of the best things about the Response Cache package is its ability to automatically remove saved responses when the information they are based on changes. It does this by using tags and events. Developers have been marking saved responses with names related to the data they contain. When that data is updated or deleted, the package automatically removes the related saved responses.
You can see the Demo Here
The package makes websites much faster. Many developers are seeing big improvements in how quickly their websites respond and how much work their servers have to do. Taylor Otwell, who created Laravel, has said good things about response caching: “The Spatie Response Cache package has been very helpful for many Laravel websites. It has been providing an easy way to make websites much faster without needing to make big changes to the code.”
The creators of the Response Cache package have not forgotten about security. The package has options to prevent saving responses from parts of the website that contain private information. This helps make sure that one user’s private information is not accidentally shown to another user.
Dr. Sarah Lee, who knows a lot about making websites fast, explains, “The Spatie Response Cache package is helping with one of the most important parts of making websites fast – reducing the time it takes for the server to respond. By saving entire responses, it has been reducing the need to do the same work over and over, which makes pages load much faster.”
Developers have found the Response Cache package especially useful for websites with a lot of content, for parts of websites that provide data to other applications, and for busy pages that do not need to show real-time information. It is working well for things like pages that list products in online stores, blog posts, and public data that other applications use. The package works absolutely errorless and well with different types of Laravel websites. It has been compatible with various versions of Laravel and has been working alongside other popular tools. This has allowed developers to add response caching to their existing websites without having to make big changes.
One thing that developers have really liked about the package is that it does not require many changes to the website’s code. They have been able to set up caching by adding just a few lines of code, usually by adding some instructions to the parts of the code that handle web requests. This simplicity has made it a good choice for both small and large websites.
While this caching package tool has remained very helpful, developers have been careful about how they use it. Saving too many responses or saving information that changes frequently can lead to showing outdated information to users. Therefore, developers have been thinking carefully about what to save and for how long to make sure their websites are both fast and accurate. The package has also been offering more advanced features for complicated situations. These include the ability to save responses based on specific conditions, save different versions of the same page for different types of users, and even save parts of responses for more detailed control.












