Skip to content

Commit d2a4cbc

Browse files
committed
Merge branch 'fix/load-times'
2 parents 77ae0f2 + 8976a6e commit d2a4cbc

13 files changed

Lines changed: 100 additions & 48 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"pixelfear/composer-dist-plugin": "^0.1.0",
2121
"scrumpy/html-to-prosemirror": "^0.6.0",
2222
"scrumpy/prosemirror-to-html": "^0.8.0",
23+
"spatie/blink": "^1.1",
2324
"statamic/stringy": "^3.1",
2425
"symfony/var-exporter": "^4.3",
2526
"symfony/yaml": "^4.1@dev",

src/Entries/Collection.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
namespace Statamic\Entries;
44

5+
use Statamic\Contracts\Entries\Collection as Contract;
6+
use Statamic\Data\ExistsAsFile;
57
use Statamic\Facades;
6-
use Statamic\Support\Arr;
7-
use Statamic\Facades\File;
8-
use Statamic\Facades\Site;
8+
use Statamic\Facades\Blink;
9+
use Statamic\Facades\Blueprint;
910
use Statamic\Facades\Entry;
11+
use Statamic\Facades\File;
1012
use Statamic\Facades\Search;
13+
use Statamic\Facades\Site;
1114
use Statamic\Facades\Stache;
12-
use Statamic\Facades\Taxonomy;
13-
use Statamic\Facades\Blueprint;
1415
use Statamic\Facades\Structure;
15-
use Statamic\Data\ExistsAsFile;
16+
use Statamic\Facades\Taxonomy;
17+
use Statamic\Support\Arr;
1618
use Statamic\Support\Traits\FluentlyGetsAndSets;
17-
use Statamic\Contracts\Entries\Collection as Contract;
1819

1920
class Collection implements Contract
2021
{
@@ -23,7 +24,6 @@ class Collection implements Contract
2324
protected $handle;
2425
protected $route;
2526
protected $mount;
26-
protected $mountedEntry;
2727
protected $title;
2828
protected $template;
2929
protected $layout;
@@ -266,6 +266,9 @@ public function save()
266266
{
267267
Facades\Collection::save($this);
268268

269+
Blink::flush('collection-handles');
270+
Blink::flushStartingWith("collection-{$this->id()}");
271+
269272
optional($this->structure())->updateEntryUris();
270273

271274
return $this;
@@ -499,7 +502,10 @@ public function structure($structure = null)
499502
return $this
500503
->fluentlyGetOrSet('structure')
501504
->getter(function ($structure) {
502-
return is_string($structure) ? Structure::findByHandle($structure) : $structure;
505+
$key = "collection-{$this->id()}-structure-{$structure}";
506+
return is_string($structure) ? Blink::once($key, function () use ($structure) {
507+
return Structure::findByHandle($structure);
508+
}) : $structure;
503509
})
504510
->args(func_get_args());
505511
}
@@ -532,7 +538,9 @@ public function mount($page = null)
532538
return $this
533539
->fluentlyGetOrSet('mount')
534540
->getter(function ($mount) {
535-
return $this->mountedEntry = $this->mountedEntry ?? Entry::find($mount);
541+
return Blink::once("collection-{$this->id()}-mount-{$mount}", function () use ($mount) {
542+
return Entry::find($mount);
543+
});
536544
})
537545
->args(func_get_args());
538546
}
@@ -542,9 +550,12 @@ public function taxonomies($taxonomies = null)
542550
return $this
543551
->fluentlyGetOrSet('taxonomies')
544552
->getter(function ($taxonomies) {
545-
return collect($taxonomies)->map(function ($taxonomy) {
546-
return Taxonomy::findByHandle($taxonomy);
547-
})->filter();
553+
$key = "collection-{$this->id()}-taxonomies-".md5(json_encode($taxonomies));
554+
return Blink::once($key, function () use ($taxonomies) {
555+
return collect($taxonomies)->map(function ($taxonomy) {
556+
return Taxonomy::findByHandle($taxonomy);
557+
})->filter();
558+
});
548559
})
549560
->args(func_get_args());
550561
}

src/Entries/Entry.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Statamic\Support\Arr;
99
use Statamic\Facades\Site;
1010
use Statamic\Facades\User;
11+
use Statamic\Facades\Blink;
1112
use Statamic\Facades\Stache;
1213
use Statamic\Facades\Blueprint;
1314
use Statamic\Routing\Routable;
@@ -78,7 +79,9 @@ public function collection($collection = null)
7879
return $collection instanceof \Statamic\Contracts\Entries\Collection ? $collection->handle() : $collection;
7980
})
8081
->getter(function ($collection) {
81-
return $collection ? Collection::findByHandle($collection) : null;
82+
return $collection ? Blink::once("collection-{$collection}", function () use ($collection) {
83+
return Collection::findByHandle($collection);
84+
}) : null;
8285
})
8386
->args(func_get_args());
8487
}
@@ -205,6 +208,10 @@ public function save()
205208

206209
Facades\Entry::save($this);
207210

211+
if ($this->id()) {
212+
Blink::store('structure-page-entries')->forget($this->id());
213+
}
214+
208215
$this->taxonomize();
209216

210217
optional(Collection::findByMount($this))->updateEntryUris();

src/Facades/Blink.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Statamic\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Blink extends Facade
8+
{
9+
protected static function getFacadeAccessor()
10+
{
11+
return \Statamic\Support\Blink::class;
12+
}
13+
}

src/Stache/Repositories/CollectionRepository.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Statamic\Stache\Repositories;
44

55
use Statamic\Stache\Stache;
6+
use Statamic\Facades\Blink;
67
use Statamic\Entries\Collection;
78
use Statamic\Events\Data\CollectionSaved;
89
use Statamic\Events\Data\CollectionDeleted;
@@ -55,7 +56,9 @@ public function make(string $handle = null): Collection
5556

5657
public function handles(): IlluminateCollection
5758
{
58-
return $this->all()->map->handle();
59+
return Blink::once('collection-handles', function () {
60+
return $this->all()->map->handle();
61+
});
5962
}
6063

6164
public function handleExists(string $handle): bool

src/Structures/Page.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
use Illuminate\Contracts\Support\Responsable;
1212
use Statamic\Contracts\Data\Augmentable;
1313
use Statamic\Data\HasAugmentedInstance;
14+
use Statamic\Facades\Blink;
1415

1516
class Page implements Entry, Augmentable, Responsable
1617
{
1718
use HasAugmentedInstance;
1819

1920
protected $tree;
2021
protected $reference;
21-
protected $entry;
2222
protected $route;
2323
protected $parent;
2424
protected $children;
@@ -80,8 +80,9 @@ public function setEntry($reference): self
8080
}
8181

8282
if (! is_string($reference)) {
83-
$this->entry = $reference;
84-
$reference = $reference->id();
83+
throw_unless($id = $reference->id(), new \Exception('Cannot set an entry without an ID'));
84+
Blink::store('structure-page-entries')->put($id, $reference);
85+
$reference = $id;
8586
}
8687

8788
$this->reference = $reference;
@@ -91,11 +92,13 @@ public function setEntry($reference): self
9192

9293
public function entry(): ?Entry
9394
{
94-
if (!$this->reference && !$this->entry) {
95+
if (! $this->reference) {
9596
return null;
9697
}
9798

98-
return $this->entry = $this->entry ?? EntryAPI::find($this->reference);
99+
return Blink::store('structure-page-entries')->once($this->reference, function () {
100+
return EntryAPI::find($this->reference);
101+
});
99102
}
100103

101104
public function reference()

src/Structures/Structure.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Statamic\Facades\Structure as StructureAPI;
1414
use Statamic\Support\Traits\FluentlyGetsAndSets;
1515
use Statamic\Contracts\Structures\Structure as StructureContract;
16+
use Statamic\Facades\Blink;
1617

1718
class Structure implements StructureContract
1819
{
@@ -25,7 +26,6 @@ class Structure implements StructureContract
2526
protected $collections;
2627
protected $maxDepth;
2728
protected $expectsRoot = false;
28-
protected $collection;
2929

3030
public function id()
3131
{
@@ -183,25 +183,11 @@ public function collections($collections = null)
183183

184184
public function collection()
185185
{
186-
if ($this->collection === false) {
187-
return null;
188-
}
189-
190-
if (is_string($this->collection)) {
191-
return Collection::findByHandle($this->collection);
192-
}
193-
194-
$collection = Collection::all()->first(function ($collection) {
195-
return $collection->structureHandle() === $this->handle();
186+
return Blink::once('structure-collection-'.$this->handle, function () {
187+
return Collection::all()->first(function ($collection) {
188+
return $collection->structureHandle() === $this->handle();
189+
});
196190
});
197-
198-
$this->collection = optional($collection)->handle() ?: false;
199-
200-
if ($collection) {
201-
return Collection::findByHandle($this->collection);
202-
}
203-
204-
return null;
205191
}
206192

207193
public function isCollectionBased()

src/Support/Blink.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Statamic\Support;
4+
5+
use Spatie\Blink\Blink as SpatieBlink;
6+
7+
class Blink
8+
{
9+
protected $stores = [];
10+
11+
public function store($name = 'default')
12+
{
13+
return $this->stores[$name] = $this->stores[$name] ?? new SpatieBlink;
14+
}
15+
16+
public function __call($method, $args)
17+
{
18+
return $this->store()->$method(...$args);
19+
}
20+
}

src/Taxonomies/Term.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Statamic\Support\Arr;
77
use Statamic\Support\Str;
88
use Statamic\Facades\Path;
9+
use Statamic\Facades\Blink;
910
use Statamic\Facades\Entry;
1011
use Statamic\Facades\Stache;
1112
use Statamic\Data\ExistsAsFile;
@@ -48,7 +49,9 @@ public function taxonomy($taxonomy = null)
4849
return $taxonomy instanceof \Statamic\Contracts\Taxonomies\Taxonomy ? $taxonomy->handle() : $taxonomy;
4950
})
5051
->getter(function ($taxonomy) {
51-
return $taxonomy ? Taxonomy::findByHandle($taxonomy) : null;
52+
return $taxonomy ? Blink::once("taxonomy-{$taxonomy}", function () use ($taxonomy) {
53+
return Taxonomy::findByHandle($taxonomy);
54+
}) : null;
5255
})
5356
->args(func_get_args());
5457
}

src/View/Antlers/Engine.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ public static function renderTag(Parser $parser, $name, $parameters = [], $conte
175175
throw $e;
176176
} catch (\Exception $e) {
177177
throw $e;
178+
} finally {
179+
debugbar()->stopMeasure($tag_measure);
178180
}
179-
180-
debugbar()->stopMeasure($tag_measure);
181181
}
182182
}

0 commit comments

Comments
 (0)