|
8 | 8 | - [Retrieving Items From the Cache](#retrieving-items-from-the-cache)
|
9 | 9 | - [Storing Items in the Cache](#storing-items-in-the-cache)
|
10 | 10 | - [Removing Items From the Cache](#removing-items-from-the-cache)
|
| 11 | + - [Cache Memoization](#cache-memoization) |
11 | 12 | - [The Cache Helper](#the-cache-helper)
|
12 | 13 | - [Atomic Locks](#atomic-locks)
|
13 | 14 | - [Managing Locks](#managing-locks)
|
@@ -325,6 +326,50 @@ Cache::flush();
|
325 | 326 | > [!WARNING]
|
326 | 327 | > Flushing the cache does not respect your configured cache "prefix" and will remove all entries from the cache. Consider this carefully when clearing a cache which is shared by other applications.
|
327 | 328 |
|
| 329 | +<a name="cache-memoization"></a> |
| 330 | +### Cache Memoization |
| 331 | + |
| 332 | +Laravel's `memo` cache driver allows you to temporarily store resolved cache values in memory during a single request or job execution. This prevents repeated cache hits within the same execution, significantly improving performance. |
| 333 | + |
| 334 | +To use the memoized cache, invoke the `memo` method: |
| 335 | + |
| 336 | +```php |
| 337 | +use Illuminate\Support\Facades\Cache; |
| 338 | + |
| 339 | +$value = Cache::memo()->get('key'); |
| 340 | +``` |
| 341 | + |
| 342 | +The `memo` method optionally accepts the name of a cache store, which specifies the underlying cache store the memoized driver will decorate: |
| 343 | + |
| 344 | +```php |
| 345 | +// Using the default cache store... |
| 346 | +$value = Cache::memo()->get('key'); |
| 347 | + |
| 348 | +// Using the Redis cache store... |
| 349 | +$value = Cache::memo('redis')->get('key'); |
| 350 | +``` |
| 351 | + |
| 352 | +The first `get` call for a given key retrieves the value from your cache store, but subsequent calls within the same request or job will retrieve the value from memory: |
| 353 | + |
| 354 | +```php |
| 355 | +// Hits the cache... |
| 356 | +Cache::memo()->get('key'); |
| 357 | + |
| 358 | +// Does not hit the cache, returns memoized value... |
| 359 | +Cache::memo()->get('key'); |
| 360 | +``` |
| 361 | + |
| 362 | +When calling methods that modify cache values (such as `put`, `increment`, `remember`, etc.), the memoized cache automatically forgets the memoized value and delegates the mutating method call to the underlying cache store: |
| 363 | + |
| 364 | +```php |
| 365 | +Cache::memo()->put('name', 'Taylor'); // Writes to underlying cache... |
| 366 | +Cache::memo()->get('name'); // Hits underlying cache... |
| 367 | +Cache::memo()->get('name'); // Memoized, does not hit cache... |
| 368 | + |
| 369 | +Cache::memo()->put('name', 'Tim'); // Forgets memoized value, writes new value... |
| 370 | +Cache::memo()->get('name'); // Hits underlying cache again... |
| 371 | +``` |
| 372 | + |
328 | 373 | <a name="the-cache-helper"></a>
|
329 | 374 | ### The Cache Helper
|
330 | 375 |
|
|
0 commit comments