Skip to content

Commit e0d6ccc

Browse files
author
phoenix
committed
perf -- load tree for field by tag once for request
1 parent e15993d commit e0d6ccc

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

src/Domain/Cache/ArrayCache.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace PhoenixLib\NovaNestedTreeAttachMany\Domain\Cache;
4+
5+
use Illuminate\Support\Collection;
6+
7+
class ArrayCache implements Cache
8+
{
9+
/** @var $cache Collection*/
10+
private $cache;
11+
12+
public function __construct(){
13+
$this->cache = Collection::make();
14+
}
15+
16+
public function get( $tag ): Collection
17+
{
18+
return $this->cache->get($tag);
19+
}
20+
21+
public function put( $tag, $data ): void
22+
{
23+
$this->cache->put($tag, $data);
24+
}
25+
26+
public function has($tag): bool
27+
{
28+
return $this->cache->has($tag);
29+
}
30+
}

src/Domain/Cache/Cache.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace PhoenixLib\NovaNestedTreeAttachMany\Domain\Cache;
4+
5+
use Illuminate\Support\Collection;
6+
7+
interface Cache
8+
{
9+
public function get( $tag ): Collection;
10+
11+
public function put( $tag, $data ): void;
12+
13+
public function has( $tag ): bool;
14+
}

src/FieldServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Support\ServiceProvider;
66
use Laravel\Nova\Events\ServingNova;
77
use Laravel\Nova\Nova;
8+
use PhoenixLib\NovaNestedTreeAttachMany\Domain\Cache\ArrayCache;
9+
use PhoenixLib\NovaNestedTreeAttachMany\Domain\Cache\Cache;
810
use PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\Handlers\BelongsToHandler;
911
use PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\Handlers\BelongsToManyHandler;
1012
use PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\Handlers\HasManyHandler;
@@ -46,5 +48,8 @@ public function register()
4648
$factory->register($this->app->make(BelongsToManyHandler::class));
4749
$factory->register($this->app->make(BelongsToHandler::class));
4850
$factory->register($this->app->make(HasManyHandler::class));
51+
52+
53+
$this->app->singleton(Cache::class, ArrayCache::class);
4954
}
5055
}

src/NestedTreeAttachManyField.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,19 @@ public function __construct($name, $attribute = null, $resource = null)
6666
'maxHeight' => 500,
6767
]);
6868

69-
$tree = $this->resourceClass::newModel()::get()
70-
->toTree();
69+
/** @var Domain\Cache\Cache $requestCache */
70+
$forRequestCache = App::make(Domain\Cache\Cache::class);
7171

72-
$this->withMeta(['options' => $tree]);
72+
$tag = get_class($this->resourceClass::newModel());
73+
74+
if(!$forRequestCache->has($tag))
75+
{
76+
$forRequestCache->put($tag, $this->resourceClass::newModel()::get()->toTree());
77+
}
78+
79+
$this->withMeta([
80+
'options' => $forRequestCache->get($tag)
81+
]);
7382
}
7483

7584
public function searchable(bool $searchable): NestedTreeAttachManyField

0 commit comments

Comments
 (0)