From: Vincent Vanwaelscappel Date: Thu, 17 Sep 2020 07:30:27 +0000 (+0200) Subject: wip #3889 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=cec50a37bb92c84f34659206ef06d552318a7ca4;p=cubist_cms-back.git wip #3889 @2 --- diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index 6f37203..29e3a21 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -12,6 +12,7 @@ use Cubist\Backpack\app\Magic\Requests\CubistMagicRequest; use Cubist\Backpack\app\Magic\Requests\CubistMagicStoreRequest; use Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest; use Cubist\Backpack\CubistBackpackServiceProvider; +use DebugBar\DebugBar; use Gaspertrix\Backpack\DropzoneField\Traits\HandleAjaxMedia; use Illuminate\Support\Arr; use Illuminate\Support\Facades\App; @@ -31,7 +32,7 @@ class CubistMagicController extends CubistCrudController protected $_oneInstance; protected $_nested = false; - protected $hidden=[]; + protected $hidden = []; public function _postSetModel() { @@ -226,7 +227,14 @@ class CubistMagicController extends CubistCrudController protected function _forgetCache() { - Cache::tags(Menu::CACHE_TAG)->flush(); + $tags = [Menu::CACHE_TAG]; + $model_instance = $this->getModelInstance(); + $classes = array_merge([get_class($model_instance)], class_parents($model_instance)); + foreach ($classes as $class) { + $tags[] = 'model_' . $class; + } + + Cache::tags($tags)->flush(); } public function index() diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index 81bc504..1b502c4 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -15,24 +15,26 @@ class Model extends Field protected function _getOptions() { - $cacheKey = $this->getAttribute('optionsmodel'); + $modelClass = $this->getAttribute('optionsmodel'); + $attr = $this->getAttribute('attribute'); + $cacheKey = '_getOption_Model_' . $modelClass . '-' . $attr; - if (!isset(static::$_options[$cacheKey])) { - static::$_options[$cacheKey] = $this->__getOptions(); + if (isset(static::$_options[$cacheKey])) { + return static::$_options[$cacheKey]; } - return static::$_options[$cacheKey]; - } - protected function __getOptions() - { - $modelClass = $this->getAttribute('optionsmodel'); - /** @var \Illuminate\Database\Eloquent\Model $inst */ - $inst = new $modelClass(); + start_measure($cacheKey, 'Get options for model ' . $modelClass . ' / ' . $attr); + $tag='model_' . $modelClass; + static::$_options[$cacheKey] = cache()->tags([$tag])->remember($cacheKey, 86400, function () use ($modelClass, $attr) { + /** @var \Illuminate\Database\Eloquent\Model $inst */ + $inst = new $modelClass(); + $options = $modelClass::all(); - $attr = $this->getAttribute('attribute'); - $options = $modelClass::all(); + return $options->pluck($attr, $inst->getKeyName())->toArray(); + }); + stop_measure($cacheKey); - return $options->pluck($attr, $inst->getKeyName())->toArray(); + return static::$_options[$cacheKey]; } public function getDatabaseType() diff --git a/src/app/Magic/Menu/Menu.php b/src/app/Magic/Menu/Menu.php index 7215fa6..cb185b1 100644 --- a/src/app/Magic/Menu/Menu.php +++ b/src/app/Magic/Menu/Menu.php @@ -64,7 +64,7 @@ class Menu extends BaseMenu } if (!isset(self::$_nav[$variant][$locale])) { - \Barryvdh\Debugbar\Facade::startMeasure('nav', 'Init Navigation object'); + start_measure('nav', 'Init Navigation object'); self::$_nav[$variant][$locale] = Cache::tags(self::CACHE_TAG)->remember('navigation_' . $variant . '_' . $locale, 43200, function () use ($locale, $variant) { $nav = new Item(); $nav->setLocale($locale); @@ -72,7 +72,7 @@ class Menu extends BaseMenu $nav->initFromDatabase(); return $nav; }); - \Barryvdh\Debugbar\Facade::stopMeasure('nav'); + stop_measure('nav'); } return self::$_nav[$variant][$locale];