What is Laravel Cache ?
What is Laravel Cache? To make your web application run faster and less CPU intensive, Laravel uses it’s built-in cache feature to store most of the data inside the cache. Data such as your api web route, config, application and views can be cached.
Laravel will automatically retrieve those data quickly from the cache when its needed. There are multiple ways or driver to store and retrieve all your cache, and the fastest way would be using redis or memcache.
To know more about Laravel cache features and how to implement it, you may visit it’s official documentation here “Cache – Laravel – The PHP Framework For Web Artisans“.
We won’t be talking about the implementation in this article, instead we will talk about how we can clear and remove data from the cache.
You see, storing data inside the cache is beneficial, however, sometimes when you added or modified your configuration or your api route, you might not see the new changes since the old data is cached and you will need to clear all your cache to get the new changes.
Laravel Cache / Artisan Cache Commands
We can clear all the cache at once or you can also choose which type of cache that you wish to clear. For example, you only want to clear your API route cache since the only changes you’ve made is your api route.
Clearing Application Route / API’s Route
Caching your application’s route will speed up your application since it doesn’t need to register all your application’s route during runtime.
If you’ve modified or added a new route in your application, you will not able to use it since it’s not inside the cache. You will need to clear and reset your route’s cache. To clear your application route simply run this command “php artisan route:clear”
$ php artisan route:clear
Route cache cleared!
If you would like to clear your route and re-cache it at the same time, you can run this command “php artisan route:cache”
$ php artisan route:cache
Route cache cleared!
Routes cached successfully!
Using this command will clear your route’s cache and then re-cache it so your application could load your new routes from the cache.
Clearing Configuration Cache
If you’ve made changes to your .env file in your Laravel project but you could not get the latest changes when you use it, you can clear your config’s cache so that it would reload and get the new changes.
To clear your configuration cache, all you need to do it running this command “php artisan config:clear” :
$ php artisan config:clear
Configuration cache cleared!
After running that command you should see this message returned “Configuration cache cleared!”
And if you want to clear and recache your config at the same time, sort of like a resetting your cache to new changes. You can run the command below :
$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!
Doing so will clear and recache your configuration at the same time.
Clearing Applications Cache
To speed up your application, you can also manually cache certain data. Using this method, we don’t have to store the data in your database. Using cache to store certain data could speed up your site’s and it’s more effective than storing and retrieving data from your database. “Cache – Laravel – The PHP Framework For Web Artisans “
These data are mostly temporary and it won’t break your site if it’s cleared. You may read more about the usage from the link above.
It is not possible to recache your application data since you control it from your coding and there are no fixed values.
To clear your application cache simply run “php artisan cache:clear”
$ php artisan cache:clear
Application cache cleared!
This command is similar to this code Cache::flush();
Using this code in your application will clear all cache. You can also choose certain cache that you want to clear by using code, for example Cache::forget('key');
Clearing Views Cache
All your views will be cached by Laravel as well so it could increase the performance when viewing your page. And by default Laravel will automatically detect if an uncompiled view has been modified more recently than a complied view and then decide if it should recompile the view.
If your recent modified views is not returning the new changes, you can clear the view’s cache by doing “php artisan view:clear”
$ php artisan view:clear
Compiled views cleared!
Again, if you would like to clear and recache your views you could do this instead “php artisan view:cache”
$ php artisan view:cache
Compiled views cleared!
Blade templates cached successfully!
This will clear your views cache and precompile all of the views utilized by your application.
Clearing Events Cache
If you are using events in your Laravel project, you can clear its cache as well. “Events – Laravel – The PHP Framework For Web Artisans“
To clear your events cache, run this command “php artisan event:clear”
$ php artisan event:clear
Cached events cleared!
To clear and rebuilt new cache for events use command “php artisan event:cache”
$ php artisan event:cache
Cached events cleared!
Events cached successfully!
Clearing All Cache
There’s one ultimate command in artisan that allows you to clear all the cache in your Laravel application except for events cache without running any of the commands mentioned above. This could save you some time and energy.
Simply run “php artisan optimize:clear”
$ php artisan optimize:clear
Cached events cleared!
Compiled views cleared!
Application cache cleared!
Route cache cleared!
Configuration cache cleared!
Compiled services and packages files removed!
Caches cleared successfully!
Conclusion for Laravel Cache
You should sometimes clear your cache while you’re working on your project. While developing your project using Laravel, you might be adding new routes or testing data values using application cache. The new changes you made might not be reflecting well on the application while testing since it loads from the cache and you could be getting the old data. Hopefully, the commands above could come in handy and useful. More programming guides here : https://www.kintechie.com/category/programming/
One thought on “Laravel Cache / Artisan Cache Commands – What you Need to Know”
Comments are closed.