Ver código fonte

Update Config

Move the work with the cache to the model.
Visman 4 anos atrás
pai
commit
7964c9d3ca
3 arquivos alterados com 24 adições e 17 exclusões
  1. 2 8
      app/Models/Config/Load.php
  2. 21 4
      app/Models/Config/Model.php
  3. 1 5
      app/Models/Config/Save.php

+ 2 - 8
app/Models/Config/Load.php

@@ -15,7 +15,7 @@ class Load extends Method
      * Заполняет модель данными из БД
      * Создает кеш
      */
-    public function load(): Config
+    public function load(): array
     {
         $config = [];
         $query  = 'SELECT cf.conf_name, cf.conf_value
@@ -40,12 +40,6 @@ class Load extends Method
             $config[$row['conf_name']] = $value;
         }
 
-        $this->model->setAttrs($config);
-
-        if (true !== $this->c->Cache->set('config', $config)) {
-            throw new RuntimeException('Unable to write value to cache - config');
-        }
-
-        return $this->model;
+        return $config;
     }
 }

+ 21 - 4
app/Models/Config/Model.php

@@ -5,6 +5,7 @@ declare(strict_types=1);
 namespace ForkBB\Models\Config;
 
 use ForkBB\Models\DataModel;
+use RuntimeException;
 
 class Model extends DataModel
 {
@@ -15,10 +16,26 @@ class Model extends DataModel
     {
         $config = $this->c->Cache->get('config');
 
-        if (\is_array($config)) {
-            $this->setAttrs($config);
-        } else {
-            $this->load();
+        if (! \is_array($config)) {
+            $config = $this->load();
+
+            if (true !== $this->c->Cache->set('config', $config)) {
+                throw new RuntimeException('Unable to write value to cache - config');
+            }
+        }
+
+        $this->setAttrs($config);
+
+        return $this;
+    }
+
+    /**
+     * Сбрасывает кеш конфига
+     */
+    public function reset(): Model
+    {
+        if (true !== $this->c->Cache->delete('config')) {
+            throw new RuntimeException('Unable to remove key from cache - config');
         }
 
         return $this;

+ 1 - 5
app/Models/Config/Save.php

@@ -6,7 +6,6 @@ namespace ForkBB\Models\Config;
 
 use ForkBB\Models\Method;
 use ForkBB\Models\Config\Model as Config;
-use RuntimeException;
 
 class Save extends Method
 {
@@ -76,10 +75,7 @@ class Save extends Method
                 $this->c->DB->exec($query, $vars);
             }
         }
-        if (true !== $this->c->Cache->delete('config')) {
-            throw new RuntimeException('Unable to remove key from cache - config');
-        }
 
-        return $this->model;
+        return $this->model->reset();
     }
 }