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

If you use multiple caches and you want to clear a specific cache, you can pass this as a parameter to the command:

You can clear cached tags with specific tags with the command:

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.

Clear cached tags with tag awesome-tag is as easy as clearing a specific cache:

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:

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:

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:

Make sure to clear this cache if you change the configuration, such as during project deployment:

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:

event: cache automatically clears all event caches, but if you have to run it under artisan you can do it like this:

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:

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:

Reference

https://beyondco.de/blog/laravel-caches-and-all-ways-to-clear-them

Share the news now