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.
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.