Laravel 12.9 Introduces Memoized Cache Driver
laravel
release
cache

Nabil Hassen
•
Apr 17, 2025

Last updated on Apr 17, 2025
Table of contents:
Laravel has added a new memoized cache driver contributed by @timacdonald that decorates existing cache drivers by storing retrieved values in memory during a request lifecycle. This allows repeated calls to the same cache key to be resolved from memory, reducing repeated calls to the underlying cache.
Basic Usage
Cache::get('foo'); // hits the cacheCache::get('foo'); // hits the cache Cache::memo()->get('foo'); // hits the cacheCache::memo()->get('foo'); // does NOT hit the cache
Value Example
Cache::put('name', 'Taylor');Cache::get('name'); // "Taylor" Cache::put('name', 'Tim');Cache::get('name'); // "Tim" Cache::put('name', 'Taylor');Cache::memo()->get('name'); // "Taylor" Cache::put('name', 'Tim');Cache::memo()->get('name'); // "Taylor"
Specifying Driver
Cache::memo()->get('name'); // default driverCache::memo('redis')->get('name'); // Redis driverCache::memo('database')->get('name'); // Database driver
Each memoized store is isolated per driver:
Cache::driver('redis')->put('name', 'Taylor in Redis');Cache::driver('database')->put('name', 'Taylor in the database'); Cache::memo('redis')->get('name'); // "Taylor in Redis"Cache::memo('database')->get('name'); // "Taylor in the database"
Mutation Behavior
Memoized drivers forget cached values when mutation methods are used:
Cache::memo()->put('name', 'Taylor'); // writes and clears memoized 'name'Cache::memo()->get('name'); // hits cacheCache::memo()->get('name'); // memory
For complete details, visit the original pull request:
🔗 laravel/framework#55304
Stay Updated.
I'll you email you as soon as new, fresh content is published.
Thanks for subscribing to my blog.