Blog Detail

10

Sep
Laravel Caches And All Ways to Clear Them cover image

Laravel Caches And All Ways to Clear Them

Some of the data retrieval or processing tasks executed by your application could be CPU intensive or take some seconds to complete. When this is the case, it is common to cache the recovered data for a time so it can be recovered quickly on subsequent requests for the same data. The cached data is usually stored in a very fast data store such as Memcached or Redis. Laravel has different caches for different parts of your application, and so there are multiple ways to clear the Laravel cache. In this article, I will tell you some ways to clear laravel caches in different ways.

Application cache

The application cache is the primary cache in Laravel. It stores everything that you manually cache in your application. You can clear only specific elements of the cache if you use tags or different cache stores. The easiest way to clear the Laravel cache is via artisan:

Clear Laravel cache via artisan command

php artisan cache:clear

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

php artisan cache:clear --store=redis

You can clear cached items with particular tags with the command:

php artisan cache:clear --tags=tag1,tag2

Clear Laravel cache Programmatically

Removing items from the cache programmatically is as easy as clearing the cache via the artisan command. Additionally, you can utilize the Cache facade to locate the cache or you can apply the cache helper.

Cache::flush()
cache()->flush()

Clearing cached items with the tag awesome-tag is as easy as purging a specific cache store:

cache()->store('redis')->tags('awesome-tag')->flush()

View cache

Another part of the application that has a cache is the view cache. The view cache stores rendered Blade templates to speed up your application. You can manually render all the views to increase the performance by employing the artisan command for it:

php artisan view:cache

If you want to utilize this optimization, you've to clear the cache if you want to deploy new code, Otherwise, Laravel uses your old views and you'll try to debug this forever. You can clear the view cache of Laravel with the command:

php artisan view:clear

Config cache

Laravel suggests caching your configuration files so that the application doesn't need to go through all config files while it bootstraps the framework.

You can join all config files into one large file and optimize the performance with the command:

php artisan config:cache

If you change a configuration, you've to make sure to clear this cache. For example, during a production deployment process:

php artisan config:clear

Event cache

When working in production, caching the Events and their Listeners enables efficient event handling. Laravel suggests caching events and listeners during your deployment process – and this means that you have to clear the event cache too.

To cache events and listeners, run the event:cache command during your deployment:

php artisan event:cache

The event:cache command automatically clears all event caches, but if you have to run it manually, you can do it like this:

php artisan event:clear

Route cache

The route cache is an additional performance cache that you only want to use in production and as part of your deployment process. Caching your routes drastically decreases the amount of time to register your application's routes. You can cache the routes via:

php artisan route:cache

If you change a route or tried the cache command during development, you have to clear the route cache otherwise your application won't find new routes. You can clear the route cache with the command:

php artisan route:clear

Published at : 10-09-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

web application development client
web application development client
web application development client
web application development client
web application development client

Launch project