Knowledge Base

Empty filter

Laravel Memoized Cache Driver

https://nabilhassen.com/laravel-129-introduces-memoized-cache-driver?utm_source=larasense.com&utm_medium=referral&utm_campaign=referral
https://github.com/laravel/framework/pull/55304

Table of contents:
Basic Usage
Value Example
Specifying Driver
Mutation Behavior
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 cache
Cache::get('foo'); // hits the cache

Cache::memo()->get('foo'); // hits the cache
Cache::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 driver
Cache::memo('redis')->get('name'); // Redis driver
Cache::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 cache
Cache::memo()->get('name'); // memory

Published
Back to Index