DEV Community

Cover image for Reduce Duplicate Cache Queries in Laravel with "Cache::memo()"
Ash Allen
Ash Allen

Posted on • Originally published at ashallendesign.co.uk

Reduce Duplicate Cache Queries in Laravel with "Cache::memo()"

Introduction

I recently wrote an article about how to use the once helper function for memoising data in Laravel applications. At the end of that article, I included a short section on alternative approaches, including the Cache::memo() method.

The Cache::memo() method is still relatively new (added to Laravel in v12.9.0 via PR #55304 by Tim MacDonald in April 2025), so I don't see it used very often in the wild. But I felt like it deserved its own article to expand on what it does and how it can be useful in your Laravel applications.

In this Quickfire article, we'll take a look at how to use the Cache::memo() method to reduce duplicated cache queries in Laravel applications to improve performance.

What is Memoisation?

To understand what the Cache::memo() method does, we first need to understand what memoisation is.

Memoisation is a technique in software and web development that involves temporarily storing the results of an expensive function call in memory so that subsequent calls can return the stored result rather than recalculating it. As a result, it can reduce the need to recompute the same value multiple times, or hit external services (like a database, API, or cache) repeatedly.

This means memoisation can sometimes provide significant performance improvements by avoiding duplicated, redundant operations.

If you want to see some other examples of memoisation in Laravel, you might be interested in checking out my "Memoisation in Laravel Using the "once" Helper" article.

Cache Memoisation with "Cache::memo()"

Now that we understand the idea behind memoisation, let's take a look at how we can use the Cache::memo() method in Laravel to reduce duplicated cache queries.

For all examples in this article, we'll assume our Laravel application uses Redis as its cache store.

To do this, let's first look at an example of duplicated cache queries that aren't using memoisation:

use Illuminate\Support\Facades\Cache;

$value = Cache::get('key'); // Hits the Redis cache
$value = Cache::get('key'); // Hits the cache again
$value = Cache::get('key'); // Hits the cache yet again
Enter fullscreen mode Exit fullscreen mode

In the example above, we can see that we're retrieving the same cache key multiple times. With each call to Cache::get('key'), we're hitting the Redis cache store again, meaning we're making three separate requests to Redis.

Of course, in a real-world application, these calls would likely be happening in different parts of your codebase, but the end result is the same: we're making multiple requests to the cache store for the same data.

Although Redis and other cache stores are typically very fast, making multiple requests for the same data is still inefficient. In a high-traffic application, these duplicate queries can quickly add up and impact performance. So if there are any ways we can reduce these duplicated queries, it's usually a good idea to do so. Remember, lots of small optimisations can add up to a big overall performance improvement.

So let's look at how we can use the Cache::memo() method to solve this problem.

use Illuminate\Support\Facades\Cache;

$value = Cache::memo()->get('key'); // Hits the Redis cache
$value = Cache::memo()->get('key'); // Returns the memoised value, no cache hit
$value = Cache::memo()->get('key'); // Returns the memoised value, no cache hit
Enter fullscreen mode Exit fullscreen mode

In the example above, we can see that we're using Cache::memo() and then calling get('key'). Using Cache::memo() tells Laravel to memoise the result of the cache query for the duration of the current request. On the first call to retrieve the cache value, Laravel will hit the Redis cache store as normal. It will then store the retrieved value in memory for the duration of the request and return the value. This means that the second and third calls will read the value from memory rather than hitting the Redis cache again.

This is great because it removes the need for us to hit the Redis cache multiple times for the same data within a single request when we request the value belonging to the key cache key.

Using Different Cache Stores

If no argument is passed to the Cache::memo() method, the default cache store defined in your Laravel application's configuration will be used. However, you can also specify a different cache store by passing the store name as an argument to the memo() method.

For example, if we wanted to explicitly use the redis cache store and memoise the values, we could do the following:

use Illuminate\Support\Facades\Cache;

Cache::memo('redis')->get('key'); // Hits the Redis cache
Cache::memo('redis')->get('key'); // Returns the memoised value, no cache hit
Enter fullscreen mode Exit fullscreen mode

Modifying Cache Values

It's important to note that if you modify a cache value using Cache::memo(), the memoised value will be forgotten. This means the next call to retrieve the value will hit the cache store again.

Take this example:

use Illuminate\Support\Facades\Cache;

Cache::memo()->put('key', 'value'); // Writes to Redis
Cache::memo()->get('name'); // Hits the Redis cache
Cache::memo()->get('name'); // Returns the memoised value, no cache hit

Cache::memo()->put('key', 'new value'); // Forgets memoised value and writes to Redis
Cache::memo()->get('name'); // Hits the Redis cache again
Cache::memo()->get('name'); // Returns the memoised value, no cache hit
Enter fullscreen mode Exit fullscreen mode

As we can see in the example above, the first Cache::memo()->get('name') call hits the Redis cache and memoises the value. The second call returns the memoised value from memory without hitting the cache again.

However, when we call Cache::memo()->put('key', 'new value'), it forgets the memoised value for that key. As a result, the next call to Cache::memo()->get('name') hits Redis again to retrieve the updated value, stores it in memory, and then returns it. The subsequent call then returns the newly memoised value.

Conclusion

Hopefully, this Quickfire article has given you a good overview of how to use the Cache::memo() method in Laravel to reduce duplicate cache queries in your applications. By leveraging memoisation, you can improve the performance of your applications by minimising redundant cache hits.

If you enjoyed reading this post, you might be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.

Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.

If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter.

Keep on building awesome stuff! 🚀

Top comments (0)