Update SmileyList

Move the work with the cache to the model.
This commit is contained in:
Visman 2020-10-30 12:00:56 +07:00
parent 614f6c87ae
commit 6d12bf18ba
2 changed files with 13 additions and 17 deletions

View file

@ -5,30 +5,19 @@ declare(strict_types=1);
namespace ForkBB\Models\SmileyList;
use ForkBB\Models\Method;
use ForkBB\Models\SmileyList\Model as SmileyList;
use PDO;
use RuntimeException;
class Load extends Method
{
/**
* Заполняет модель данными из БД
* Создает кеш
* Загружает данные из БД
*/
public function load(): SmileyList
public function load(): array
{
$query = 'SELECT id, sm_code, sm_image, sm_position
FROM ::smilies
ORDER BY sm_position';
$list = $this->c->DB->query($query)->fetchAll(PDO::FETCH_UNIQUE);
$this->model->list = $list;
if (true !== $this->c->Cache->set('smilies', $list)) {
throw new RuntimeException('Unable to write value to cache - smilies');
}
return $this->model;
return $this->c->DB->query($query)->fetchAll(PDO::FETCH_UNIQUE);
}
}

View file

@ -11,15 +11,22 @@ class Model extends ParentModel
{
/**
* Загружает список смайлов из кеша/БД
* Создает кеш
*/
public function init(): Model
{
$this->list = $this->c->Cache->get('smilies');
$list = $this->c->Cache->get('smilies');
if (! \is_array($this->list)) {
$this->load();
if (! \is_array($list)) {
$list = $this->load();
if (true !== $this->c->Cache->set('smilies', $list)) {
throw new RuntimeException('Unable to write value to cache - smilies');
}
}
$this->list = $list;
return $this;
}