Skip to content

Commit

Permalink
Merge branch 'fix/load-times'
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Feb 14, 2020
2 parents 77ae0f2 + 8976a6e commit d2a4cbc
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 48 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"pixelfear/composer-dist-plugin": "^0.1.0",
"scrumpy/html-to-prosemirror": "^0.6.0",
"scrumpy/prosemirror-to-html": "^0.8.0",
"spatie/blink": "^1.1",
"statamic/stringy": "^3.1",
"symfony/var-exporter": "^4.3",
"symfony/yaml": "^4.1@dev",
Expand Down
37 changes: 24 additions & 13 deletions src/Entries/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace Statamic\Entries;

use Statamic\Contracts\Entries\Collection as Contract;
use Statamic\Data\ExistsAsFile;
use Statamic\Facades;
use Statamic\Support\Arr;
use Statamic\Facades\File;
use Statamic\Facades\Site;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Entry;
use Statamic\Facades\File;
use Statamic\Facades\Search;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Facades\Taxonomy;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Structure;
use Statamic\Data\ExistsAsFile;
use Statamic\Facades\Taxonomy;
use Statamic\Support\Arr;
use Statamic\Support\Traits\FluentlyGetsAndSets;
use Statamic\Contracts\Entries\Collection as Contract;

class Collection implements Contract
{
Expand All @@ -23,7 +24,6 @@ class Collection implements Contract
protected $handle;
protected $route;
protected $mount;
protected $mountedEntry;
protected $title;
protected $template;
protected $layout;
Expand Down Expand Up @@ -266,6 +266,9 @@ public function save()
{
Facades\Collection::save($this);

Blink::flush('collection-handles');
Blink::flushStartingWith("collection-{$this->id()}");

optional($this->structure())->updateEntryUris();

return $this;
Expand Down Expand Up @@ -499,7 +502,10 @@ public function structure($structure = null)
return $this
->fluentlyGetOrSet('structure')
->getter(function ($structure) {
return is_string($structure) ? Structure::findByHandle($structure) : $structure;
$key = "collection-{$this->id()}-structure-{$structure}";
return is_string($structure) ? Blink::once($key, function () use ($structure) {
return Structure::findByHandle($structure);
}) : $structure;
})
->args(func_get_args());
}
Expand Down Expand Up @@ -532,7 +538,9 @@ public function mount($page = null)
return $this
->fluentlyGetOrSet('mount')
->getter(function ($mount) {
return $this->mountedEntry = $this->mountedEntry ?? Entry::find($mount);
return Blink::once("collection-{$this->id()}-mount-{$mount}", function () use ($mount) {
return Entry::find($mount);
});
})
->args(func_get_args());
}
Expand All @@ -542,9 +550,12 @@ public function taxonomies($taxonomies = null)
return $this
->fluentlyGetOrSet('taxonomies')
->getter(function ($taxonomies) {
return collect($taxonomies)->map(function ($taxonomy) {
return Taxonomy::findByHandle($taxonomy);
})->filter();
$key = "collection-{$this->id()}-taxonomies-".md5(json_encode($taxonomies));
return Blink::once($key, function () use ($taxonomies) {
return collect($taxonomies)->map(function ($taxonomy) {
return Taxonomy::findByHandle($taxonomy);
})->filter();
});
})
->args(func_get_args());
}
Expand Down
9 changes: 8 additions & 1 deletion src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Statamic\Support\Arr;
use Statamic\Facades\Site;
use Statamic\Facades\User;
use Statamic\Facades\Blink;
use Statamic\Facades\Stache;
use Statamic\Facades\Blueprint;
use Statamic\Routing\Routable;
Expand Down Expand Up @@ -78,7 +79,9 @@ public function collection($collection = null)
return $collection instanceof \Statamic\Contracts\Entries\Collection ? $collection->handle() : $collection;
})
->getter(function ($collection) {
return $collection ? Collection::findByHandle($collection) : null;
return $collection ? Blink::once("collection-{$collection}", function () use ($collection) {
return Collection::findByHandle($collection);
}) : null;
})
->args(func_get_args());
}
Expand Down Expand Up @@ -205,6 +208,10 @@ public function save()

Facades\Entry::save($this);

if ($this->id()) {
Blink::store('structure-page-entries')->forget($this->id());
}

$this->taxonomize();

optional(Collection::findByMount($this))->updateEntryUris();
Expand Down
13 changes: 13 additions & 0 deletions src/Facades/Blink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Facades;

use Illuminate\Support\Facades\Facade;

class Blink extends Facade
{
protected static function getFacadeAccessor()
{
return \Statamic\Support\Blink::class;
}
}
5 changes: 4 additions & 1 deletion src/Stache/Repositories/CollectionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Stache\Repositories;

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

public function handles(): IlluminateCollection
{
return $this->all()->map->handle();
return Blink::once('collection-handles', function () {
return $this->all()->map->handle();
});
}

public function handleExists(string $handle): bool
Expand Down
13 changes: 8 additions & 5 deletions src/Structures/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
use Illuminate\Contracts\Support\Responsable;
use Statamic\Contracts\Data\Augmentable;
use Statamic\Data\HasAugmentedInstance;
use Statamic\Facades\Blink;

class Page implements Entry, Augmentable, Responsable
{
use HasAugmentedInstance;

protected $tree;
protected $reference;
protected $entry;
protected $route;
protected $parent;
protected $children;
Expand Down Expand Up @@ -80,8 +80,9 @@ public function setEntry($reference): self
}

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

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

public function entry(): ?Entry
{
if (!$this->reference && !$this->entry) {
if (! $this->reference) {
return null;
}

return $this->entry = $this->entry ?? EntryAPI::find($this->reference);
return Blink::store('structure-page-entries')->once($this->reference, function () {
return EntryAPI::find($this->reference);
});
}

public function reference()
Expand Down
24 changes: 5 additions & 19 deletions src/Structures/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Statamic\Facades\Structure as StructureAPI;
use Statamic\Support\Traits\FluentlyGetsAndSets;
use Statamic\Contracts\Structures\Structure as StructureContract;
use Statamic\Facades\Blink;

class Structure implements StructureContract
{
Expand All @@ -25,7 +26,6 @@ class Structure implements StructureContract
protected $collections;
protected $maxDepth;
protected $expectsRoot = false;
protected $collection;

public function id()
{
Expand Down Expand Up @@ -183,25 +183,11 @@ public function collections($collections = null)

public function collection()
{
if ($this->collection === false) {
return null;
}

if (is_string($this->collection)) {
return Collection::findByHandle($this->collection);
}

$collection = Collection::all()->first(function ($collection) {
return $collection->structureHandle() === $this->handle();
return Blink::once('structure-collection-'.$this->handle, function () {
return Collection::all()->first(function ($collection) {
return $collection->structureHandle() === $this->handle();
});
});

$this->collection = optional($collection)->handle() ?: false;

if ($collection) {
return Collection::findByHandle($this->collection);
}

return null;
}

public function isCollectionBased()
Expand Down
20 changes: 20 additions & 0 deletions src/Support/Blink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Statamic\Support;

use Spatie\Blink\Blink as SpatieBlink;

class Blink
{
protected $stores = [];

public function store($name = 'default')
{
return $this->stores[$name] = $this->stores[$name] ?? new SpatieBlink;
}

public function __call($method, $args)
{
return $this->store()->$method(...$args);
}
}
5 changes: 4 additions & 1 deletion src/Taxonomies/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Statamic\Facades\Path;
use Statamic\Facades\Blink;
use Statamic\Facades\Entry;
use Statamic\Facades\Stache;
use Statamic\Data\ExistsAsFile;
Expand Down Expand Up @@ -48,7 +49,9 @@ public function taxonomy($taxonomy = null)
return $taxonomy instanceof \Statamic\Contracts\Taxonomies\Taxonomy ? $taxonomy->handle() : $taxonomy;
})
->getter(function ($taxonomy) {
return $taxonomy ? Taxonomy::findByHandle($taxonomy) : null;
return $taxonomy ? Blink::once("taxonomy-{$taxonomy}", function () use ($taxonomy) {
return Taxonomy::findByHandle($taxonomy);
}) : null;
})
->args(func_get_args());
}
Expand Down
4 changes: 2 additions & 2 deletions src/View/Antlers/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ public static function renderTag(Parser $parser, $name, $parameters = [], $conte
throw $e;
} catch (\Exception $e) {
throw $e;
} finally {
debugbar()->stopMeasure($tag_measure);
}

debugbar()->stopMeasure($tag_measure);
}
}
4 changes: 3 additions & 1 deletion tests/Data/Entries/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,11 @@ function it_gets_sort_field_and_direction()
/** @test */
function it_saves_the_collection_through_the_api()
{
$collection = new Collection;
$collection = (new Collection)->handle('test');

Facades\Collection::shouldReceive('save')->with($collection)->once();
Facades\Blink::shouldReceive('flush')->with('collection-handles')->once();
Facades\Blink::shouldReceive('flushStartingWith')->with('collection-test')->once();

$return = $collection->save();

Expand Down
5 changes: 4 additions & 1 deletion tests/Data/Entries/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,13 @@ function it_gets_the_blueprint_based_on_the_collection()
function it_saves_through_the_api()
{
Event::fake();
$entry = (new Entry)->collection(new Collection);
$entry = (new Entry)->id('a')->collection(new Collection);
Facades\Entry::shouldReceive('save')->with($entry);
Facades\Entry::shouldReceive('taxonomize')->with($entry);

$blinkStore = $this->mock(\Spatie\Blink\Blink::class)->shouldReceive('forget')->with('a')->once()->getMock();
Facades\Blink::shouldReceive('store')->with('structure-page-entries')->once()->andReturn($blinkStore);

$return = $entry->save();

$this->assertTrue($return);
Expand Down
8 changes: 4 additions & 4 deletions tests/Data/Structures/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PageTest extends TestCase
function it_gets_and_sets_the_entry()
{
$page = new Page;
$entry = new Entry;
$entry = (new Entry)->id('a');
$this->assertNull($page->entry());

$return = $page->setEntry($entry);
Expand Down Expand Up @@ -77,7 +77,7 @@ function it_builds_a_uri_based_on_the_position_in_the_structure_when_the_structu
{
$entry = new class extends Entry {
public function id($id = null) {
return '1';
return 'a';
}
public function slug($slug = null) {
return 'entry-slug';
Expand Down Expand Up @@ -109,7 +109,7 @@ function it_gets_the_entrys_uri_when_the_structure_does_not_have_a_collection()
{
$entry = new class extends Entry {
public function id($id = null) {
return '1';
return 'a';
}
public function uri() {
return '/the/actual/entry/uri';
Expand All @@ -134,7 +134,7 @@ function it_gets_child_pages()

$page = (new Page)
->setTree($tree)
->setEntry(new Entry)
->setEntry((new Entry)->id('123'))
->setRoute('')
->setChildren([
['entry' => 'one'],
Expand Down

0 comments on commit d2a4cbc

Please sign in to comment.