Laravel provides in-built functionality for caching. A cache is used for better application performance. Laravel has a predefined function for handling cache operations like creating retrieving and deleting the cache. Before starting let’s understand cache :
What is cache?
A cache is a special storage space for temporary files that makes a device, browser, or app run faster and more efficiently. When a user opens the application for the first time then Laravel creates a cache automatically.
Why is cache Used?
Generally, Cache is used to increase data retrieval performance by reducing the need to access the underlying slower storage layer. A cache can also be used for storing temporary data.
Laravel provides in-built functionality for caching. A cache is used for better application performance. Laravel has a predefined function for handling cache operations like creating retrieving and deleting the cache. Before starting let’s understand cache.
As mentioned Laravel Framework automatically generates cache files. But Sometimes, we need to clear those cache files.
For example, we have a home page of our application and used it for a while but later we made some other changes. In application cache files old version of your home page is stored whenever an existing users try to access that page there are chances for showing the old page. This problem can be solved by clearing view cache files.
In Laravel we can clear caches by two methods:
- Using Terminal
- Using Browser
Let’s take examples for them one by one:
Clear Cache in Laravel Application using Terminal
Generally, Cache is used to increase data retrieval performance by reducing the need to access the underlying slower storage layer. A cache can also be used for storing temporary data.
Clear Route Cache
Laravel automatically caches all routes but when you make changes to a route. you will face an error while accessing the new page. To clear route cache enter below command:
Clear Configuration Cache
Below Command will clear the configuration cache which contains application configuration like database configuration, mail configuration, queue configuration, view configuration, and much more.
Clear View Cache
Compiling views during the request may have a small negative impact on performance, so Laravel provides the view:cache Artisan command to precompile all of the views utilized by your application. You can clear the view cache by below command:
Clear All Cache at Once
Laravel has a wildcard command to clear all types of caches like applications, routes, views, configuration, and files.
Clear Cache in Laravel Application using Browser
Sometimes your hosting service provider/plan doesn’t provide terminal or SSH access. Then we can use this method to clear cache files using a browser.
As you know, you can run any console command using Artisan Facade in Laravel. So we can use routing to run those commands programmatically. We just need to define routes for this task. Here is an example of a sample route to clear caches by its type: