ItSolutionStuff.com

Laravel Improve Site Performance By Caching Entire Response

By Hardik Savani • April 16, 2024
Laravel

we always want to speed up our website to load and try to improve performance using cache. in this tutorial i will explain how to make faster load site in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

If you are working on blog website or some content website then i think you have same response always in blade file. so, if we cache an entire response then it make faster your site. here we will use laravel-responsecache composer package for cache response.

laravel-responsecache provide several option to cache response. if you don't want to cache response on some routes then you can use their middleware. Also you can simple configuration by responsecache.php config file. also you can simply clear cache by their command.

First we have to install laravel-responsecache composer package by following command, so just run bellow command on your project root directory.

composer require spatie/laravel-responsecache

After run successfully command, you have to run bellow command to create config file.

php artisan vendor:publish --provider="Spatie\ResponseCache\ResponseCacheServiceProvider"

now you can see responsecache.php file in config folder.

You have to use middleware in Kernel.php file. so add like as bellow:

app/Http/Kernel.php

...

protected $middlewareGroups = [

'web' => [

...

\Spatie\ResponseCache\Middlewares\CacheResponse::class,

],

...

protected $routeMiddleware = [

...

'doNotCacheResponse' => \Spatie\ResponseCache\Middlewares\DoNotCacheResponse::class,

];

You can clear response cache by following command:

php artisan responsecache:clear

If you do not want to cache some routes then use middleware like as bellow:

Route::get('/auth/logout', ['middleware' => 'doNotCacheResponse', 'uses' => 'AuthController@getLogout']);

you can specify the number of minutes these routes should be cached by following way:

Route::get('/my-special-snowflake', 'SnowflakeController@index')->middleware('cacheResponse:5');

You can try and check it...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Install JQuery UI in Laravel Vite?

Read Now →

How to Install JQuery in Laravel Vite?

Read Now →

How to Read Content from PDF File in Laravel?

Read Now →

How to Install Bootstrap 5 in Laravel 10?

Read Now →

Laravel Array Length Validation Example

Read Now →

Laravel Eloquent Order By Length Query Example

Read Now →

FCM Push Notification in Laravel Example

Read Now →

How to Run All Seeders in Laravel?

Read Now →

Laravel US State Seeder Example

Read Now →

Laravel Seeder from CSV File Example

Read Now →

How to Get Data Between Two Dates in Laravel?

Read Now →

Laravel Carbon Subtract Seconds Example

Read Now →

Laravel Improve Speed Performance using Model Caching Tutorial

Read Now →

How to optimize website speed and performance in Laravel?

Read Now →

Laravel Create Quick Backend Admin Panel Tutorial

Read Now →