HeadlinesBriefing favicon HeadlinesBriefing.com

Laravel Cache::memo() Reduces Duplicate Queries

DEV Community •
×

Laravel's Cache::memo() method, added in v12.9.0 by Tim MacDonald, tackles duplicate cache queries within a single request. It memoizes the result of a cache lookup in memory, so subsequent calls for the same key hit memory instead of the cache store again. This prevents redundant requests to services like Redis.

This technique is crucial for high-traffic applications where repeated cache hits for the same data can add up. While Redis is fast, eliminating these duplicate operations provides a tangible performance boost. It builds on Laravel's existing memoisation tools, like the `once` helper, offering a dedicated cache-layer solution.

Developers can specify a cache store, like `Cache::memo('redis')->get('key')`, or rely on the default. A key detail: modifying a memoized value via `put()` forgets the cached version, forcing the next `get()` to hit the cache store again. This ensures data consistency while still optimizing read-heavy patterns.