Laravel Cache Management: Clear the Cache Like a Pro!
Laravel as php framework, provides a strong caching service to enhance the performance and user experience. This caching feature is one of the reason that made this framework so popular among the dev community. But alongside that, when developing application in this framework, you might sometimes experience that your updates in the codes arent reflecting properly. For example, you have made some changes in routes but those updated routes are saying “not found” or working as expected. So these issues might be triggering from the caching. For solution, you might want to use some quick artisan commands to clear up the caches. In this article we will have some knowledge on clearing or storing caches in laravel efficiently.
Types of caching
Laravel uses different types of caching for increasing the speed and performance in the app. Here are the list of the main caching mechanisms in Laravel:
1. Route cache: Caches all the route informations.
2. View cache: Caches the view informations including the .blade files.
3. Config cache: Stores caching information on configuration data.
4. Events cache: (Optional) Caches event listeners and their associated handlers for optimized event dispatching.
5. Application cache: Stores compiled class files (.php) generated by PHP's opcode cache.
How can I clear these caches when needed?
To clear caches as we need, we can run commands individually for route, view or configs etc. Or we can do those combinedly in a single command. Lets see examples.
To clear route cache, you should run:
php artisan route:clear
For view caching removal, you can run:
php artisan view:clear
To clear config cache, you can run command:
php artisan config:clear
For clearing application cache, here is the command you should run:
php artisan cache:clear
Now if you want to clear all the caches at once, we can run:
php artisan optimize:clear
What if I want to cache those again?
You might wonder, if I clear those caches and now I want to start caching again, how I can do that, right? Just replace the part ‘clear’ with ‘cache’ in those commands. Here are the examples for starting caching routes, views, configs or all again:
php artisan route:cache //Caches the routes
php artisan view:cache //Caches the views
php artisan config:cache //Caches the configs
php artisan optimize //Caches for all
How to clear caches from browser
Sometimes you might want to trigger these commands from browser. For example, in a shared hosting, where you cant run php artisan commands to start or clear your caches, you might face difficulties. To resolve that, we can trigger artisan commands from routes too. Here is an example:
Route::get('clear-cache', function() {
Artisan::call('cache:clear');
Artisan::call('config:clear');
Artisan::call('view:clear');
Artisan::call('optimize:clear');
return "Cache cleared!";
});
Now if you hit the route 'clear-cache', all the caches for your application will be cleared at once.
Summary
So in summary, whenever you are facing something unusual, try clearing up the caches, most of the time these issues get resolved by just clearing the caches. These commands will help you in every step for laravel development. The point of caching is to build up enhanced performance for the application, but also we have to ensure that, these caching dont become source of any other bugs so that we can provide users an error-free experience. Its recommended to clear up the caches whenever you make a big changes to the application.
So thats all from here today. Thank you for reading this post. Hope you found this helpful. Let me know your thoughts in the comments and please give a love to this article.
I wish you a happy day.
Comments