Stop Slow Laravel Apps! Optimize with Smart Caching

Raiyan Memon
3 min readJan 29, 2025

--

Caching is the most underrated part of our application. It is used to retrieve most fetched data which does not change frequently.

Caching can reduce the load of our CPU and respond quickly. It minimizes the interaction with the database so our server does not get fully loaded.

It retrieves the complex queried data quickly, reducing the response time considerably.

We can set the cache for some time or forever as per our needs.

The most common Cache used in Laravel is:

  1. file (default)
  2. Memcached
  3. Redis.

Let’s dive into the implementation of Cache in our Laravel application.

The functionality of the cache is to retrieve an item from the cache, but also store a value if the requested key doesn’t exist

In this post will talk about the simplest form of Cache that will be used in our day-to-day applications.

1. Storing and Retrieve Cache for some time interval

Here we are going to store the cache for some amount of time and when the given time is exceeded it automatically deletes the cache, fetches the new value and store it again for the same time.

Let’s see the code.

use Illuminate\Support\Facades\Cache;

$seconds = 180;
$users = Cache::remember('allUsers', $seconds, function () {
return User::all();
});
return response([
'success' => true,
'data' => $users,
]);

The code above stores all the user data for 3 minutes (180 seconds) in “allUsers” key and afther 3 minutes it deletes the key and value.

Working of this code: Retrieve all users from the cache or, if they don’t exist, retrieve them from the database and add them to the cache for 3 minutes

2. Storing Cache forever.

Here we are going to store the cache forever, which means that it will never delete the cache automatically.

use Illuminate\Support\Facades\Cache;

$users = Cache::rememberForever('allUsers', function () {
return User::all();
});
return response([
'success' => true,
'data' => $users,
]);

The code above stores all the user data forever in “allUsers” key.

Working of this code: Retrieve all users from the cache or, if they don’t exist, retrieve them from the database and add them to the cache forever.

3. Deleting the Cache

To delete the cache just delete the key, doesn’t matter if the cache is stored for some time or forever.

use Illuminate\Support\Facades\Cache;

Cache::forget('allUsers');

Above code deletes the “allUsers” cache.

Taking the real-time example:

use Illuminate\Support\Facades\Cache; 

public function setting()
{
$data = Cache::rememberForever('business-settings', function () {
return BusinessSetting::all();
});
return response([
'success' => true,
'data' => $data,
]);
}

Created an API for fetching my business settings if the “business-settings” key is there it gets called the “BusinessSetting” model it will just return the data from the cache.

use Illuminate\Support\Facades\Cache; 

public function store(Request $request)
{
foreach ($request->all() as $key => $value) {
BusinessSetting::updateOrCreate(
[
'key' => $key,
],
[
'value' => $value,
]
);
}
Cache::forget('business-settings');
return response([
'header' => 'Updated!',
'message' => 'Settings Updated Successfully'
]);
}

From my panel storing the business setting and removing the “business-settings” cache so that when the user calls the get API for business setting it stores the latest settings in the cache and gets the updated version of it.

Finally, this is how simply a cache is implemented in our Laravel application.

Have recommendations or thoughts? Feel free to share them in the comments!

Want to know more about me or get in touch? Visit my website.

Keep coding and exploring ❤️

--

--

Raiyan Memon
Raiyan Memon

Written by Raiyan Memon

Software Engineer & Full Stack Web Developer working with Laravel and Nextjs

No responses yet