Performance Optimization in Laravel Applications: Caching in Laravel

Adrian Generous
3 min readJul 4, 2024

Welcome to the first episode of a three-part series on code optimization in Laravel. Today, we’ll dive into the topic of caching and how it can help improve the performance of our applications.

Why Caching?

When building web applications, we often encounter performance issues. Each server request may require many database operations, view generation, and other complex tasks. Caching allows storing the results of these operations, enabling quick access to them without having to repeat the same tasks.

Configuring Caching in Laravel

Laravel supports various cache systems like Redis, Memcached, and even simple file-based caching. To configure caching, simply edit the config/cache.php configuration file.

return [
'default' => env('CACHE_DRIVER', 'file'),

'stores' => [
'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache/data'),
],

'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
],
];

We can easily change the default cache driver by setting the CACHE_DRIVER value in the .env file:

CACHE_DRIVER=redis

--

--

Adrian Generous

I navigate between coding & marketing, experienced as a CTO, project manager, & ad agency owner. I share Laravel & more on Medium, also a history enthusiast.