Skip to content

Commit

Permalink
Merge pull request #1 from novius/feature/cache-system
Browse files Browse the repository at this point in the history
Create cache system
  • Loading branch information
tonyyb authored Sep 3, 2019
2 parents d10a592 + 97e4dd7 commit f0f203f
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 3 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions config/nova-order-nestedset-field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'cache_enabled' => false,
];
15 changes: 13 additions & 2 deletions src/OrderNestedsetField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion src/OrderNestedsetFieldServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -36,7 +38,10 @@ public function boot()
*/
public function register()
{
//
$this->mergeConfigFrom(
__DIR__.'/../config/nova-order-nestedset-field.php',
'nova-order-nestedset-field'
);
}

/**
Expand Down
29 changes: 29 additions & 0 deletions src/Traits/Orderable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -14,6 +18,9 @@ public function moveOrderUp()
}
}

/**
* Move item to next position
*/
public function moveOrderDown()
{
$nextItem = $this->getNextSibling();
Expand All @@ -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');
}
}

0 comments on commit f0f203f

Please sign in to comment.