Update Config
Move the work with the cache to the model.
This commit is contained in:
parent
a333387636
commit
7964c9d3ca
3 changed files with 24 additions and 17 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue