Skip to content

Commit 12d9cec

Browse files
committed
[6.x] Speed up Stache warming for large structured collections
1 parent ae63c74 commit 12d9cec

5 files changed

Lines changed: 20 additions & 25 deletions

File tree

src/Entries/Entry.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,7 @@ public function order()
561561
return $this->value('order');
562562
}
563563

564-
return $this->structure()->in($this->locale())
565-
->flattenedPages()
566-
->map->reference()
567-
->flip()->get($this->id) + 1;
564+
return $this->structure()->in($this->locale())->entryOrder($this->id) + 1;
568565
}
569566

570567
public function template($template = null)

src/Structures/CollectionStructure.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,13 @@ public function collection()
2727
});
2828
}
2929

30-
private function flattenedPages($entry)
31-
{
32-
return Blink::once('collection-structure-flattened-pages-collection'.$this->handle().'-'.$entry->locale(), function () use ($entry) {
33-
return $this->in($entry->locale())->flattenedPages();
34-
});
35-
}
36-
3730
public function entryUri($entry)
3831
{
3932
if (! $this->route($entry->locale())) {
4033
return null;
4134
}
4235

43-
$page = $this->flattenedPages($entry)
44-
->keyBy->reference()
45-
->get($entry->id());
36+
$page = $this->in($entry->locale())->findByEntry($entry->id());
4637

4738
$page?->setEntry($entry);
4839

src/Structures/Pages.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ public function flattenedPages()
9292

9393
foreach ($this->all() as $page) {
9494
$flattened->push($page);
95-
$flattened = $flattened->merge($page->flattenedPages());
95+
96+
foreach ($page->flattenedPages() as $descendant) {
97+
$flattened->push($descendant);
98+
}
9699
}
97100

98101
return $flattened;

src/Structures/Tree.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ abstract class Tree implements ContainsQueryableValues, Contract, Localization
2222
protected $locale;
2323
protected $tree = [];
2424
protected $cachedFlattenedPages;
25+
protected $cachedFlattenedPagesById;
26+
protected $cachedFlattenedPagesByReference;
27+
protected $cachedFlattenedPageOrder;
2528
protected $withEntries = false;
2629
protected $uriCacheEnabled = true;
2730

@@ -143,17 +146,17 @@ public function uriCacheEnabled()
143146

144147
public function find($id): ?Page
145148
{
146-
return $this->flattenedPages()
147-
->keyBy->id()
148-
->get($id);
149+
return ($this->cachedFlattenedPagesById ??= $this->flattenedPages()->keyBy->id())->get($id);
149150
}
150151

151152
public function findByEntry($id)
152153
{
153-
return $this->flattenedPages()
154-
->filter->reference()
155-
->keyBy->reference()
156-
->get($id);
154+
return ($this->cachedFlattenedPagesByReference ??= $this->flattenedPages()->filter->reference()->keyBy->reference())->get($id);
155+
}
156+
157+
public function entryOrder($reference)
158+
{
159+
return ($this->cachedFlattenedPageOrder ??= $this->flattenedPages()->map->reference()->flip())->get($reference);
157160
}
158161

159162
public function save()
@@ -163,8 +166,10 @@ public function save()
163166
}
164167

165168
$this->cachedFlattenedPages = null;
169+
$this->cachedFlattenedPagesById = null;
170+
$this->cachedFlattenedPagesByReference = null;
171+
$this->cachedFlattenedPageOrder = null;
166172

167-
Blink::forget('collection-structure-flattened-pages-collection*');
168173
Blink::forget('collection-structure-tree*');
169174

170175
$this->repository()->save($this);

tests/Data/Structures/CollectionStructureTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,13 @@ public function it_gets_an_entry_uri()
174174
$this->collection->shouldReceive('route')->with('en')->once()->andReturn('{slug}');
175175

176176
$page = $this->mock(Page::class);
177-
$page->shouldReceive('reference')->andReturn('the-entry-id');
178177
$page->shouldReceive('setEntry')->once()->andReturnSelf();
179178
$page->shouldReceive('uri')->andReturn('/the-uri-from-the-page');
180179

181180
$tree = $this->mock(Tree::class);
182181
$tree->shouldReceive('structure')->andReturn($structure);
183182
$tree->shouldReceive('locale')->andReturn('en');
184-
$tree->shouldReceive('flattenedPages')->andReturn(collect([$page]));
183+
$tree->shouldReceive('findByEntry')->with('the-entry-id')->andReturn($page);
185184

186185
CollectionTreeRepository::shouldReceive('find')->with('test', 'en')->andReturn($tree);
187186

0 commit comments

Comments
 (0)