]> _ Git - cubist_cms-back.git/commitdiff
wip #3889 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 17 Sep 2020 07:30:27 +0000 (09:30 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 17 Sep 2020 07:30:27 +0000 (09:30 +0200)
src/app/Magic/Controllers/CubistMagicController.php
src/app/Magic/Fields/Model.php
src/app/Magic/Menu/Menu.php

index 6f37203aeeb83786c0744de5c6ff7a743264689c..29e3a21e2058fc86306c6634ebbf5af0882df1cf 100644 (file)
@@ -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()
index 81bc504d738887ef3de8dc6e686c03ee42374c71..1b502c4767ac6ec96bed6ad547fc972374b96d33 100644 (file)
@@ -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()
index 7215fa6a502525c1ded083b0c8ad2771c2dfea8c..cb185b16c3368aa39d812d67a0090e2f4183caa7 100644 (file)
@@ -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];