Skip to content

Commit a3f5921

Browse files
committed
memo docs
1 parent da87313 commit a3f5921

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

cache.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [Retrieving Items From the Cache](#retrieving-items-from-the-cache)
99
- [Storing Items in the Cache](#storing-items-in-the-cache)
1010
- [Removing Items From the Cache](#removing-items-from-the-cache)
11+
- [Cache Memoization](#cache-memoization)
1112
- [The Cache Helper](#the-cache-helper)
1213
- [Atomic Locks](#atomic-locks)
1314
- [Managing Locks](#managing-locks)
@@ -325,6 +326,50 @@ Cache::flush();
325326
> [!WARNING]
326327
> 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.
327328
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+
328373
<a name="the-cache-helper"></a>
329374
### The Cache Helper
330375

0 commit comments

Comments
 (0)