From 26167c0d0f6f979a9b7aaa5c69e6df3110691364 Mon Sep 17 00:00:00 2001 From: Oliver Mesieh Date: Wed, 26 Jun 2024 11:39:24 +0200 Subject: [PATCH] Optimize performance when gathering content pages Content gathering could take a long time for large content bases. This commit makes these calls concurrent if `spatie/fork` is available. --- src/Generator.php | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Generator.php b/src/Generator.php index 4263184..2f7351a 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -8,6 +8,7 @@ use Illuminate\Filesystem\Filesystem; use Illuminate\Routing\Router; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Cache; use League\Flysystem\Filesystem as Flysystem; use League\Flysystem\Local\LocalFilesystemAdapter; use Statamic\Contracts\Imaging\UrlBuilder; @@ -244,16 +245,37 @@ protected function gatherContent($urls = '*') protected function gatherAllPages() { - return collect() + $pages = collect() ->merge($this->routes()) ->merge($this->urls()) ->merge($this->entries()) ->merge($this->terms()) ->merge($this->scopedTerms()) - ->values() - ->unique - ->url() - ->reject(fn ($page) => $this->shouldRejectPage($page)); + ->values(); + + $closures = $this->makeCachePagesClosures($pages); + + $results = $this->tasks->run(...$closures); + + return collect($results)->flatMap(function ($key) { + return Cache::pull($key); + })->unique->url(); + } + + protected function makeCachePagesClosures($pages) + { + return $pages->split($this->workers)->map(function ($pages, $key) { + return function () use ($pages, $key) { + $cacheKey = "ssg::pages::{$key}"; + + $pages = $pages + ->reject(fn ($page) => $this->shouldRejectPage($page)); + + Cache::put($cacheKey, $pages); + + return $cacheKey; + }; + }); } protected function makeContentGenerationClosures($pages, $request)