From 0426e1cf5af73d4abd5ae5aeba07d5d8b49efd49 Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Wed, 21 Jan 2026 11:28:46 +0100 Subject: [PATCH] Minimize memory usage for retrieving product rewrite URLs --- src/Commands/InvalidateCacheCommand.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Commands/InvalidateCacheCommand.php b/src/Commands/InvalidateCacheCommand.php index 6885ffc..4cad6b4 100644 --- a/src/Commands/InvalidateCacheCommand.php +++ b/src/Commands/InvalidateCacheCommand.php @@ -75,20 +75,22 @@ public function handle(Cacher $cacher, Writer $writer): void protected function addProductsUrls(): self { - $products = config('rapidez.models.product')::withoutGlobalScopes() + $productIds = config('rapidez.models.product')::withoutGlobalScopes() ->where('updated_at', '>=', $this->latestCheck) ->orWhereIn('entity_id', $this->getUpdatedStockProducts()) - ->with(['parent:entity_id' => ['rewrites']]) - ->with('rewrites') - ->get('entity_id'); + ->pluck('entity_id'); - foreach ($products as $product) { - $this->addUrls($this->getUrlsFromRewrites($product->rewrites)); + $parentIds = config('rapidez.models.product_link')::whereIn('product_id', $productIds) + ->groupBy('parent_id') + ->pluck('parent_id'); - if ($product->parent) { - $this->addUrls($this->getUrlsFromRewrites($product->parent->rewrites)); - } - } + $allIds = $productIds->merge($parentIds)->unique(); + + $rewrites = config('rapidez.models.rewrite')::whereIn('entity_id', $allIds) + ->where('entity_type', 'product') + ->pluck('request_path'); + + $this->addUrls($rewrites); return $this; }