Speed Up Your Laravel App with Memoized Cache in Laravel 12.9

Boost Laravel performance using the new memoized cache driver in Laravel 12.9. Learn how it works, when to use it, and see real-world examples for faster caching.

Speed Up Your Laravel App with Memoized Cache in Laravel 12.9 Image

Introduction

With Laravel 12.9, a new experimental memoized cache driver was introduced to supercharge application performance. If you've ever wished your Laravel app could skip repetitive cache reads and deliver lightning-fast responses, the memoized driver is the answer.

In this article, we’ll explore:

  • What is the memoized cache driver?
  • How it works under the hood
  • Real-world use cases
  • Configuration and code examples
  • Performance considerations

What is the Memoized Cache Driver in Laravel?

The memoized cache driver stores data in memory during a single request lifecycle, meaning it doesn’t hit the underlying cache (e.g., Redis or file) again if the same cache key is requested multiple times within the same request.

It’s not a persistent cache—it’s an in-request memory layer on top of your actual cache store.

Why Use It?

Laravel developers often call cache()->get('some_key') multiple times in a request. If the key was already fetched, why fetch it again from Redis or the file system? The memoized driver remembers previous cache calls and returns them instantly.


Real-World Use Cases

Use Case 1: Avoid Repeated Cache Fetches in API Controllers

public function show($id)
{
    // First call
    $user = Cache::get("user_{$id}"); 
    // Later in the same request
    $profile = Cache::get("user_{$id}"); // No Redis hit if memoized
    // Normally, both would hit Redis
    return response()->json([$user, $profile]);
}

With the memoized cache driver, the second get() is served from memory, not Redis.


Use Case 2: Performance Boost in Blade Views with Shared Data

view()->share('popular_posts', Cache::get('popular_posts'));
// In layout.blade.php
@foreach($popular_posts as $post)
   {{ $post->title }}
@endforeach 

In complex Blade layouts, the same cache key might be accessed multiple times. Memoization ensures Laravel doesn’t re-fetch it from Redis or files.


Use Case 3: Complex Service Classes or Event Listeners

In service classes where multiple methods rely on the same cache key:

class UserService {
    public function getUser($id)
    {
        return Cache::get("user_{$id}");
    }
    public function getUserProfile($id)
    {
        $user = $this->getUser($id); // Already memoized
        return $user->profile;
    }
}

How to Enable Memoized Cache in Laravel 12.9

Laravel 12.9 introduced the memoize() method on the cache repository.

Here’s how to use it:

use Illuminate\Support\Facades\Cache;
$cache = Cache::memoize();
$data = $cache->get('some_key', function () {
    return 'cached value';
});

Chaining with Other Drivers

You can wrap any cache driver with memoization:

$memoizedRedis = Cache::store('redis')->memoize();
$value = $memoizedRedis->get('key');

You don’t need to change your default cache store—just memoize when needed.


Full Example: Memoized Caching in a Controller

use Illuminate\Support\Facades\Cache;
class ProductController extends Controller
{
    public function index()
    {
        $cache = Cache::store('redis')->memoize();
        $featured = $cache->remember('featured_products', 600, function () {
            return Product::featured()->take(10)->get();
        });
        $latest = $cache->remember('latest_products', 600, function () {
            return Product::latest()->take(10)->get();
        });
        // If featured and latest both call same model methods, memoized cache helps
        return view('products.index', compact('featured', 'latest'));
    }
}

Memoized vs Persistent Cache

FeatureMemoized CacheRedis/File Cache
PersistenceOnly for current requestPersistent across requests
SpeedFastest (in-memory)Fast but requires IO/network
Use caseRepeated same-key accessGlobal/shared caching
ScopePer-requestApplication-wide

When Not to Use Memoized Cache?

  • Don’t use it alone for long-term caching. It resets every request.
  • It won't improve performance across requests—only within a single request.
  • Avoid relying on it for data consistency or cross-request availability.

Performance Tip: Combine Both Caches

You can combine persistent cache with memoized wrapper:

$cache = Cache::store('redis')->memoize();
$data = $cache->remember('settings', 3600, function () {
    return Setting::all();
});

This caches settings in Redis but avoids repeated Redis calls in the same request.


Conclusion

Laravel’s new memoized cache driver in v12.9 brings a powerful in-memory caching layer for performance optimization. While it's not a replacement for Redis or file caching, it eliminates redundant cache lookups during a request—saving time and resources.

Quick Benefits Recap:

  • Increases performance by reducing cache calls
  • Ideal for repeated Cache::get() calls
  • Easy to implement with Cache::memoize()
  • Works with any existing cache driver (Redis, file, etc.)

If you're searching for:

  • How to use Laravel memoized cache
  • Laravel cache performance tips
  • Laravel 12.9 cache driver examples
  • Memoize in Laravel
  • In-memory caching in Laravel

Then this is the practical guide you need to improve caching speed in your Laravel 12+ applications.


 Try It Now

Start memoizing your cache calls today and see measurable speed improvements in your app!

Do you Like?