How to Clear Cache in Laravel

Updated: January 16, 2024 By: Guest Contributor Post a comment

Introduction

In this tutorial, we’ll explore the various ways you can clear the cache in a Laravel application. Caching is a vital aspect of any web application for speeding up response times and enhancing overall performance. Laravel provides an elegant API to manage caching which, when utilized correctly, can lead to a significant boost in performance.

Understanding Laravel Cache

Before we proceed with clearing the cache, it’s important to understand the types of cache that Laravel uses:

  • Application Cache: Stores user-defined data.
  • Route Cache: Compilation of your application’s routes for faster route registration.
  • Config Cache: Compilation of all your configuration files.
  • View Cache: Compiled versions of your Blade templates.
  • Event Cache: Cache of your event listeners.

Clearing Application Cache

The easiest way to clear your application cache is by running the following Artisan command:

php artisan cache:clear

This command will clear all data stored in the cache. After running the command, you should see the following output:

Cache cleared successfully.

Clearing Route Cache

To clear the route cache, use the following Artisan command:

php artisan route:clear

This will clear the route cache and you should see the following confirmation message:

Route cache cleared!

Clearing Config Cache

The configuration cache can be cleared as follows:

php artisan config:clear

Running this command removes the configuration cache and shows:

Configuration cache cleared!

Clearing Compiled Views

To clear the compiled views, you would issue this command:

php artisan view:clear

This will clear all compiled Blade templates. You should see:

Compiled views cleared!

Clearing Cached Events

In Laravel, you can also cache your event listeners for a slight boost in performance. To clear your event cache, you can use:

php artisan event:clear

Upon running it, you’ll receive this output:

Event cache cleared!

Clear Multiple Caches in One Go

If you need to clear several different types of cache, you can combine the commands:

php artisan cache:clear && php artisan route:clear && php artisan config:clear && php artisan view:clear

This will sequentially clear the application, route, config, and view caches in one line of code.

Custom Cache Clearing Commands

In more complex applications, you might need to clear cache strategically or conditionally. You can write custom artisan commands to suit your requirements:

php artisan make:command ClearCustomCache

After running the above command, you need to define its functionality in the generated class found in app/Console/Commands/ClearCustomCache.php. Here, you can leverage the Cache facade to clear specific keys or tags from the cache:

public function handle()
{
    Cache::forget('key-name');
    // Or for tags
    Cache::tags(['tag1', 'tag2'])->flush();
    $this->info('Custom cache cleared successfully.');
}

Advanced Cache Clearing Techniques

In distributed systems, you might need to clear the cache in response to events from another service. Here, you can use Laravel’s event broadcasting feature to listen for specific events and clear the cache:

Event::listen(JobPostedEvent::class, function (
{
    Cache::tags(['jobs', 'user-' . auth()->id()])->flush();
});

You can use queues to defer the cache clearing, thereby not interfering with the user’s experience:

dispatch(function () {
    Cache::tags('long-running')->flush();
})->delay(now()->addMinutes(10));

By leveraging Laravel’s caching strategies efficiently, not only can you boost your application’s performance, but also ensure it remains responsive during high-load scenarios.

Conclusion

In essence, clearing cache in Laravel is a simple procedure that can greatly influence your application’s performance. From clearing the cache globally to targeted cache purging, Laravel offers you a variety of tools to keep your application optimized and responsive.