diff --git a/README.md b/README.md index 54b360f..4794616 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,14 @@ A field that make your resources orderable using [the laravel nestedset package] composer require novius/laravel-nova-order-nestedset-field ``` +### Configuration + +Some options that you can override are available. + +```sh +php artisan vendor:publish --provider="Novius\LaravelNovaOrderNestedsetField\OrderNestedsetFieldServiceProvider" --tag="config" +``` + ## Usage **Step 1** @@ -95,6 +103,56 @@ need to specify this attribute as scope attribute: [Retrieve more information about usage on official doc](https://github.com/lazychaser/laravel-nestedset#scoping). +## Performances + +You can enable cache to avoid performance issues in case of large tree. + +**By default cache is disabled.** + +To use cache you have to enabled it in config file with : + +```php +return [ + ... + + 'cache_enabled' => true, +]; +``` + +**You have to clear cache on every tree updates with an observer on your Model (or directly in boot method).** + +```php +namespace App\Models; + +use Illuminate\Database\Eloquent\Model; +use Kalnoy\Nestedset\NodeTrait; +use Novius\LaravelNovaOrderNestedsetField\Traits\Orderable; + +class Foo extends Model +{ + use NodeTrait; + use Orderable; + + public static function boot() + { + parent::boot(); + + if (config('nova-order-nestedset-field.cache_enabled', false)) { + static::created(function (Theme $model) { + $model->clearOrderableCache(); + }); + + static::updated(function (Theme $model) { + $model->clearOrderableCache(); + }); + + static::deleted(function (Theme $model) { + $model->clearOrderableCache(); + }); + } + } +} +``` ## Override default languages files diff --git a/config/nova-order-nestedset-field.php b/config/nova-order-nestedset-field.php new file mode 100644 index 0000000..762908a --- /dev/null +++ b/config/nova-order-nestedset-field.php @@ -0,0 +1,5 @@ + false, +]; diff --git a/src/OrderNestedsetField.php b/src/OrderNestedsetField.php index cab13a6..266c04a 100644 --- a/src/OrderNestedsetField.php +++ b/src/OrderNestedsetField.php @@ -2,6 +2,7 @@ namespace Novius\LaravelNovaOrderNestedsetField; +use Illuminate\Support\Facades\Cache; use Kalnoy\Nestedset\NodeTrait; use Laravel\Nova\Fields\Field; use Novius\LaravelNovaOrderNestedsetField\Traits\Orderable; @@ -46,8 +47,18 @@ protected function resolveAttribute($resource, $attribute) ])); } - $first = $resource->buildSortQuery()->ordered()->first(); - $last = $resource->buildSortQuery()->ordered('desc')->first(); + if (config('nova-order-nestedset-field.cache_enabled', false)) { + $cachePrefix = $resource->getOrderableCachePrefix(); + $first = Cache::rememberForever($cachePrefix.'.first', function () use ($resource) { + return $resource->buildSortQuery()->ordered()->first(); + }); + $last = Cache::rememberForever($cachePrefix.'.last', function () use ($resource) { + return $resource->buildSortQuery()->ordered('desc')->first(); + }); + } else { + $first = $resource->buildSortQuery()->ordered()->first(); + $last = $resource->buildSortQuery()->ordered('desc')->first(); + } $this->withMeta([ 'first' => is_null($first) ? null : $first->id, diff --git a/src/OrderNestedsetFieldServiceProvider.php b/src/OrderNestedsetFieldServiceProvider.php index f5daf34..4faa71a 100644 --- a/src/OrderNestedsetFieldServiceProvider.php +++ b/src/OrderNestedsetFieldServiceProvider.php @@ -25,6 +25,8 @@ public function boot() Nova::style('laravel-nova-order-nestedset-field', __DIR__.'/../dist/css/field.css'); }); + $this->publishes([__DIR__.'/../config' => config_path()], 'config'); + $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'nova-order-nestedset-field'); $this->publishes([__DIR__.'/../resources/lang' => resource_path('lang/vendor/nova-order-nestedset-field')], 'lang'); } @@ -36,7 +38,10 @@ public function boot() */ public function register() { - // + $this->mergeConfigFrom( + __DIR__.'/../config/nova-order-nestedset-field.php', + 'nova-order-nestedset-field' + ); } /** diff --git a/src/Traits/Orderable.php b/src/Traits/Orderable.php index 9b61028..3121f99 100644 --- a/src/Traits/Orderable.php +++ b/src/Traits/Orderable.php @@ -3,9 +3,13 @@ namespace Novius\LaravelNovaOrderNestedsetField\Traits; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Facades\Cache; trait Orderable { + /** + * Move item to previous position + */ public function moveOrderUp() { $prevItem = $this->getPrevSibling(); @@ -14,6 +18,9 @@ public function moveOrderUp() } } + /** + * Move item to next position + */ public function moveOrderDown() { $nextItem = $this->getNextSibling(); @@ -36,8 +43,30 @@ public function buildSortQuery() return $query; } + /** + * @param Builder $query + * @param string $direction + * @return Builder + */ public function scopeOrdered(Builder $query, string $direction = 'asc') { return $query->orderBy($this->getLftName(), $direction); } + + /** + * @return string + */ + public function getOrderableCachePrefix(): string + { + return sprintf('nova-order-nestedset-field.%s', md5(get_class($this).'-'.(int) $this->{$this->getParentIdName()})); + } + + /** + * Clear the cache corresponding to the model + */ + public function clearOrderableCache() + { + Cache::forget($this->getOrderableCachePrefix().'.first'); + Cache::forget($this->getOrderableCachePrefix().'.last'); + } }