Skip to content

Commit

Permalink
Optimize performance when gathering content pages
Browse files Browse the repository at this point in the history
Content gathering could take a long time for large content bases. This commit makes these calls concurrent if `spatie/fork` is available.
  • Loading branch information
o1y committed Jun 26, 2024
1 parent 3bb1658 commit 26167c0
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 26167c0

Please sign in to comment.