From: Vincent Vanwaelscappel Date: Thu, 16 Jun 2022 07:19:37 +0000 (+0200) Subject: wip #4210 @1.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=b22b03d848d0881e51543bd7a5619a16e00e5d1e;p=cubist_cms-back.git wip #4210 @1.5 --- diff --git a/src/app/Console/Commands/PrecacheCommand.php b/src/app/Console/Commands/PrecacheCommand.php new file mode 100644 index 0000000..0a3e5a0 --- /dev/null +++ b/src/app/Console/Commands/PrecacheCommand.php @@ -0,0 +1,28 @@ +_handleMagicFolder([$this, '_precache']); + } + + /** + * @param $model CubistMagicAbstractModel + */ + public function _precache($model) + { + $model->preCache(true); + } +} diff --git a/src/app/CubistBackpackServiceProvider.php b/src/app/CubistBackpackServiceProvider.php index 6be34f8..8a81bd4 100644 --- a/src/app/CubistBackpackServiceProvider.php +++ b/src/app/CubistBackpackServiceProvider.php @@ -7,6 +7,7 @@ use Cubist\Backpack\Console\Commands\GenerateCommand; use Cubist\Backpack\Console\Commands\InstallCommand; use Cubist\Backpack\Console\Commands\LocaleCopy; use Cubist\Backpack\Console\Commands\LocaleSlugReset; +use Cubist\Backpack\Console\Commands\PrecacheCommand; use Cubist\Backpack\Console\Commands\RefreshComposedAttributesCommand; use Cubist\Backpack\Console\Commands\SearchIndexCommand; use Cubist\Backpack\Console\Commands\UpdateCommand; @@ -80,7 +81,8 @@ class CubistBackpackServiceProvider extends ServiceProvider RefreshComposedAttributesCommand::class, SearchIndexCommand::class, LocaleCopy::class, - LocaleSlugReset::class + LocaleSlugReset::class, + PrecacheCommand::class ]); } } diff --git a/src/app/Http/Controllers/Operations/CloneEditOperation.php b/src/app/Http/Controllers/Operations/CloneEditOperation.php index 5df97fd..c55cb64 100644 --- a/src/app/Http/Controllers/Operations/CloneEditOperation.php +++ b/src/app/Http/Controllers/Operations/CloneEditOperation.php @@ -27,24 +27,20 @@ trait CloneEditOperation $clonedEntry->name .= ' (copy)'; $clonedEntry->save(); $newid = $clonedEntry->id; - $url = Route::current()->uri . '?' . request()->getQueryString(); - $url = str_replace('clone-edit', 'edit', $url); - $url = str_replace('{id}', $newid, $url); - if (isset($_GET['ajax'])) { + /** @var CubistMagicAbstractModel $entry */ $entry = call_user_func([$_GET['entry'], 'find'], $_GET['id']); $field = $entry->getField($_GET['name']); - if ($field instanceof Model) { - $options = $field->_getOptions(); - } else if ($field instanceof SelectFromArray) { - $options = $field->getOptions(); - } else { - $options = []; - } - SelectFromArray::hashOptions($options, $_GET['ajax']); + $modelAttribute=$field->getAttribute('attribute'); + $v=$clonedEntry->getAttributeValue($modelAttribute); + $d=[$newid => $v]; + SelectFromArray::prependHashOptions($d, $_GET['ajax']); } + $url = Route::current()->uri . '?' . request()->getQueryString(); + $url = str_replace('clone-edit', 'edit', $url); + $url = str_replace('{id}', $newid, $url); return response()->redirectTo($url); } } diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index ae29387..ad147d9 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -24,7 +24,12 @@ class Model extends Field return $this->___options; } - public function pluck($attr) + public function preCache($force = false) + { + $this->pluck($this->getAttribute('attribute'), true, $force); + } + + public function pluck($attr, $onlyGlobal = false, $force = false) { $bui = backpack_user() === null ? '' : backpack_user()->id; @@ -34,17 +39,14 @@ class Model extends Field $modelOrderByWay = $this->getAttribute('optionsmodel_orderby_way', 'asc'); $baseCacheKey = '_getOption_Model_' . $modelClass . '-|' . $modelScope; $globalCacheKey = $baseCacheKey . '|-' . $attr; - $cacheKey = $globalCacheKey . '|-' . $bui; - $cacheKeysKey = $baseCacheKey . '/KEYS/' . '|-' . $bui . '|-' . '!' . $modelOrderBy . '!' . $modelOrderByWay . '!!'; - if (isset(static::$_options[$cacheKey])) { - return static::$_options[$cacheKey]; - } $tag = 'model_' . $modelClass; - - if (!isset(static::$_options[$globalCacheKey])) { + if ($force || !isset(static::$_options[$globalCacheKey])) { + if ($force) { + cache()->forget($globalCacheKey); + } start_measure($globalCacheKey, 'Get options for model ' . $modelClass . ' / ' . $attr); - static::$_options[$globalCacheKey] = cache()->tags([$tag])->remember($globalCacheKey, 86400, function () use ($modelClass, $attr, $modelScope, $modelOrderBy, $modelOrderByWay) { + static::$_options[$globalCacheKey] = cache()->tags([$tag])->remember($globalCacheKey, 86400, function () use ($modelClass, $attr, $modelScope) { set_time_limit(0); /** @var \Illuminate\Database\Eloquent\Model $inst */ $inst = new $modelClass(); @@ -53,21 +55,32 @@ class Model extends Field if ($modelScope) { $q = $q->$modelScope(); } - if ($modelOrderBy) { - $q->orderBy($modelOrderBy, $modelOrderByWay); - } return $q->where('created_ok', 1)->get()->pluck($attr, $inst->getKeyName())->toArray(); }); stop_measure($globalCacheKey); } + if ($onlyGlobal) { + return; + } + + $cacheKey = $globalCacheKey . '|-' . $bui; + $cacheKeysKey = $baseCacheKey . '/KEYS/|-' . $bui . '|-!' . $modelOrderBy . '!' . $modelOrderByWay . '!!'; + + if (isset(static::$_options[$cacheKey])) { + return static::$_options[$cacheKey]; + } + $globalScopes = $this->getAttribute('optionsmodel_global_scopes', true); if (!$globalScopes) { static::$_options[$cacheKey] = static::$_options[$globalCacheKey]; return static::$_options[$cacheKey]; } - if (!isset(static::$_options[$cacheKeysKey])) { + if ($force || !isset(static::$_options[$cacheKeysKey])) { + if ($force ) { + cache()->forget($cacheKeysKey); + } static::$_options[$cacheKeysKey] = cache()->tags([$tag])->remember($cacheKeysKey, 86400, function () use ($modelClass, $modelOrderBy, $modelOrderByWay) { set_time_limit(0); /** @var Builder $q */ @@ -79,6 +92,9 @@ class Model extends Field }); } + if ($force) { + cache()->forget($cacheKey); + } static::$_options[$cacheKey] = cache()->tags([$tag])->remember($cacheKey, 86400, function () use ($globalCacheKey, $cacheKeysKey) { $res = []; foreach (static::$_options[$cacheKeysKey] as $k) { diff --git a/src/app/Magic/Fields/SelectFromArray.php b/src/app/Magic/Fields/SelectFromArray.php index ac7ac27..5c67a26 100644 --- a/src/app/Magic/Fields/SelectFromArray.php +++ b/src/app/Magic/Fields/SelectFromArray.php @@ -51,16 +51,15 @@ class SelectFromArray extends Field return $hash; } -// public function getDatabaseLength() -// { -// $keys = array_keys($this->getAttribute('options')); -// $max = 0; -// foreach ($keys as $key) { -// $max = max($max, mb_strlen($key)); -// } -// return round($max * 1.5); -// } - + public static function prependHashOptions($options, $hash) + { + $data = []; + foreach ($options as $k => $v) { + $data[$k] = ['t' => $v, 'n' => self::normalize($v)]; + } + $data = $data + Cache::get('select2_' . $hash); + Cache::put('select2_' . $hash, $data, 6000); + } public function getOptions() { diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 262a070..4c5e195 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -1053,12 +1053,12 @@ class CubistMagicAbstractModel extends Model implements HasMedia return $this->editActions; } - public function preCache() + public function preCache($force = false) { foreach ($this->getFields() as $field) { if ($field instanceof \Cubist\Backpack\Magic\Fields\Model) { /** @var $field \Cubist\Backpack\Magic\Fields\Model */ - $field->_getOptions(); + $field->preCache($force); } $field->getColumnData(); $field->getFilterValues();