Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Memoizing the cache in Laravel

When you cache a value in Laravel, it’s stored in the cache store (like Redis, Memcached, etc.) and can be retrieved later. This is great since it prevents expensive operations like database queries or API calls.

use Illuminate\Support\Facades\Cache;

Cache::put('name', 'Cherika');
$name = Cache::get('name');

However, when you’re using the cached value multiple times in your code, you’re still hitting the cache store each time you call Cache::get(). This isn’t free since maybe your cache store lives on a different server and every time you hit it, it has to make a network call to retrieve the value.

The memoize driver

To fix this issue, Laravel now has a driver to memoize the cache value. This means that when you retrieve a value from the cache, it will be stored in memory for the duration of the request. This way, you can access the cached value multiple times without hitting the cache store each time.

use Illuminate\Support\Facades\Cache;

Cache::put('name', 'Cherika');
$name = Cache::get('name'); // Hits the cache store
$name = Cache::get('name'); // Hits the cache store

$name = Cache::memo()->get('name'); // Hits the cache store
$name = Cache::memo()->get('name'); // Does not hit the cache store

As you can see, the first two calls to Cache::get() hit the cache store each time. But memoizing the cache value with Cache::memo()->get() only hits the cache store the first time. The second call to Cache::memo()->get() retrieves the value from memory, which is much faster than hitting the cache store again.

Memoized values does not mutate

When you memoize a value, it is stored in memory for the duration of the request. This means that if you change the value in the cache store, it will not affect the memoized value.

use Illuminate\Support\Facades\Cache;

Cache::put('name', 'Cherika');
$name = Cache::memo()->get('name'); // Cherika

Cache::put('name', 'John Doe'); // Change the value in the cache store
$name = Cache::memo()->get('name'); // Cherika

This is to make sure that the memoized value is consistent throughout the request which is important for performance and reliability.

Custom memoize driver

By default, Laravel uses in-memory caching for memoizing values. This means that the memoized values are stored in memory for the duration of the request. However, you can also use a custom driver to store the memoized values in a different way.

// Redis driver...
Cache::memo('redis')->get('name');

// Database driver...
Cache::memo('database')->get('name');

Each driver will get a unique memo decorator. This means values are not shared between different drivers.

Cache::driver('redis')->put('item', 'This is in Redis');
Cache::driver('database')->put('item', 'This is in the database');

Cache::memo('redis')->get('name'); // "This is in Redis"
Cache::memo('database')->get('name'); // "This is in the database"

This can be convenient if you want to use a different caching strategy for memoized values. For example, you might want to use a database driver for memoizing values that are expensive to compute, while using the default in-memory driver for other values.

Conclusion

Memoizing the cache in Laravel is a great way to improve performance and reduce the number of network calls to the cache store. By using the Cache::memo() method, you can store values in memory for the duration of the request and avoid hitting the cache store multiple times. This can be especially useful for expensive operations like database queries or API calls.

Learn more about this feature in this PR.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, 8.3, and 8.4), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?