Laravel's memoized cache, Cache::memo(), reads a key from your cache store once and serves it from memory for the rest of the request or job. Starting in Laravel 13.33, it also works with cache tags, so you can memoize tagged values with Cache::memo()->tags().
This helps when one request reads the same tagged value many times. A permission lookup might run for every can() call and every @can directive in a view, and without memoization each one goes back to the cache store.
#Memoizing a Tagged Lookup
Call tags() on the memoized cache and use it like any other tagged cache:
use Illuminate\Support\Facades\Cache;
public function getPermissions(User $user): Collection
{
return Cache::memo()
->tags(['permissions', "user:{$user->id}"])
->remember(
"permissions:{$user->id}",
now()->addHour(),
fn () => $this->loadPermissionsFromDatabase($user)
);
}
The first call in a request reads from the cache store, or runs the closure on a cache miss. Later calls with the same tags and key return the value from memory.
Before the memoized cache supported tags, the workaround was to wrap the tagged cache in an array store yourself:
return Cache::store('array')->tags('permission_cache')->rememberForever($cacheKey,
fn () => Cache::tags($this->cacheTags($user))->remember(
$cacheKey,
config('auth.permissions.default_cache_time'),
fn () => $this->loadPermissionsFromDatabase($user)
)
);
#Writes and Flushes
Methods that change a value write to the tagged cache and drop the memoized copy of that key, so the next read goes back to the store:
$cache = Cache::memo()->tags(['permissions']);
$cache->get('permissions:1'); // Reads from the cache store
$cache->get('permissions:1'); // Reads from memory
$cache->put('permissions:1', $updated); // Writes to the store, forgets the memoized value
$cache->get('permissions:1'); // Reads from the cache store again
This covers put(), putMany(), add(), increment(), decrement(), forever(), touch(), and forget(). Calling flush() on a memoized tagged cache flushes the tags in the store and clears the memoized tagged values.
#When to Use It
The memoized cache holds what the current request has already read. If a queue worker or another request changes the value, this request does not see the change.
I would use it for values that do not change during a request, like permissions or settings. Keep counters and anything another process updates mid-request on the regular cache.
The memoized values reset at the start of each request or job, so long-running workers do not keep stale data between jobs.
#Further Reading
- Tagged Memoized Cache and Model Refreshes in Laravel 13.33, the release that added
tags() - Memoized Cache Driver in Laravel 12.9, where
Cache::memo()first shipped - Interactively Search and Delete Cache Keys in Laravel with Cache UI
- Cache memoization in the Laravel documentation
- Pull request #61593 by Joost de Bruijn
Комментарии (0)
Пока нет комментариев — будьте первым.