Cache in Laravel and all the ways to clear it
- Tram Ho
Laravel has different caches for different parts of your application and so there are many ways to clear the Laravel cache.
Application Cache
The application cache is the main cache in Laravel. It stores everything that you manually cache in your application. You can only delete specific cache elements if you use different tags or cache stores. The easiest way to clear the Laravel cache is through artisan:
Clear Laravel cache via artisan . command
1 2 | php artisan cache:clear |
If you use multiple caches and you want to clear a specific cache, you can pass this as a parameter to the command:
1 2 | php artisan cache:clear --store=redis |
You can clear cached tags with specific tags with the command:
1 2 | php artisan cache:clear --tags=tag1,tag2 |
Clear Laravel cache with code
Deleting tags from the cache with code is as easy as clearing the cache via the artisan command. Alternatively, you may use the cache facade to access the cache or use the cache helper. Deleting items from the cache by code is as easy as clearing the cache via the artisan command.
1 2 3 | Cache::flush() cache()->flush() |
Clear cached tags with tag awesome-tag
is as easy as clearing a specific cache:
1 2 | cache()->store('redis')->tags('awesome-tag')->flush() |
Whenever you want to check if an item is in the cache or remove it from the cache, start Tinkerwell and run the commands above.
View cache
Another part of the application that has a cache is the view cache. View cache stores rendered Blade templates to speed up your application. You can expose all views to increase performance by using artisan command for it:
1 2 | php artisan view:cache |
If you use this optimization, you must clear the cache if you deploy new code, otherwise Laravel uses your old view and you will try to debug this bug forever. You can clear Laravel’s view cache with the command:
1 2 | php artisan view:clear |
Configuration cache
Laravel recommends caching your configuration files so that the application doesn’t need to go through all the configuration files during framework startup.
You can combine all the configuration files into one large file and optimize performance with the command:
1 2 | php artisan config:cache |
Make sure to clear this cache if you change the configuration, such as during project deployment:
1 2 | php artisan config:clear |
Event cache
When run in a project, caching events and listeners allows for efficient event handling. Laravel recommends caching events and listeners in your implementation – and this means you have to clear the event cache as well. To cache events and listeners, run the event: cache command in your implementation:
1 2 | php artisan event:cache |
event: cache
automatically clears all event caches, but if you have to run it under artisan you can do it like this:
1 2 | php artisan event:clear |
Route Caching
The route cache is an additional performance cache that you only want to use during the run of a project and as part of your deployment. Caching your routes greatly reduces the time it takes to register your application’s routes. You can cache routes via:
1 2 | php artisan route:cache |
In case you change the route or try the cache command during development, you must clear the route cache or your application will not find the new routes. You clear the route cache with the command:
1 2 | php artisan route:clear |
Reference
https://beyondco.de/blog/laravel-caches-and-all-ways-to-clear-them