<!--
title: Cache expensive operations
domain: app-frameworks
topic: Performance Optimization
language: PHP
source: laravel/framework
updated: 2025-06-19
url: https://awesomereviewers.com/reviewers/framework-cache-expensive-operations/
-->

Identify and cache results of expensive operations that may be called repeatedly during a request lifecycle, especially recursive functions or operations performed in loops. This reduces redundant processing and can significantly improve application performance.

For example, when working with trait detection or class hierarchy traversal:

```php
// Instead of repeatedly calling an expensive function
public function isPrunable(): bool
{
    return in_array(Prunable::class, class_uses_recursive(static::class)) || static::isMassPrunable();
}

// Cache the result for the lifetime of the request
protected static $classTraitsCache = [];

public function isPrunable(): bool
{
    $class = static::class;
    
    // Only perform the expensive operation once per class
    if (!isset(static::$classTraitsCache[$class])) {
        static::$classTraitsCache[$class] = class_uses_recursive($class);
    }
    
    return in_array(Prunable::class, static::$classTraitsCache[$class]) || static::isMassPrunable();
}
```

Similarly, when selecting methods for array operations, favor efficient approaches. For instance, use `array_search()` over `array_flip()` followed by key lookup for better performance with large arrays.

When implementing expensive transformations (like `toArray()` on models), use memoization techniques to avoid recalculating the same values multiple times during a single request.
