Update Censorship

Move the work with the cache to the model.
This commit is contained in:
Visman 2020-10-30 10:20:19 +07:00
parent 5f6e3b99c5
commit de3df36af6
3 changed files with 27 additions and 23 deletions

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace ForkBB\Models\Censorship;
use ForkBB\Models\Model as ParentModel;
use RuntimeException;
class Model extends ParentModel
{
@ -16,12 +17,16 @@ class Model extends ParentModel
if ('1' == $this->c->config->o_censoring) {
$list = $this->c->Cache->get('censorship');
if (isset($list['searchList'], $list['replaceList'])) {
$this->searchList = $list['searchList'];
$this->replaceList = $list['replaceList'];
} else {
$this->refresh();
if (! isset($list['searchList'], $list['replaceList'])) {
$list = $this->refresh();
if (true !== $this->c->Cache->set('censorship', $list)) {
throw new RuntimeException('Unable to write value to cache - censorship');
}
}
$this->searchList = $list['searchList'];
$this->replaceList = $list['replaceList'];
}
return $this;
@ -38,4 +43,16 @@ class Model extends ParentModel
return $str;
}
}
/**
* Сбрасывает кеш цензуры
*/
public function reset(): Model
{
if (true !== $this->c->Cache->delete('censorship')) {
throw new RuntimeException('Unable to remove key from cache - censorship');
}
return $this;
}
}

View file

@ -5,8 +5,6 @@ declare(strict_types=1);
namespace ForkBB\Models\Censorship;
use ForkBB\Models\Method;
use ForkBB\Models\Censorship\Model as Censorship;
use RuntimeException;
class Refresh extends Method
{
@ -14,7 +12,7 @@ class Refresh extends Method
* Заполняет модель данными из БД
* Создает кеш
*/
public function refresh(): Censorship
public function refresh(): array
{
$query = 'SELECT ce.id, ce.search_for, ce.replace_with
FROM ::censoring AS ce';
@ -28,17 +26,10 @@ class Refresh extends Method
. ')(?![\p{L}\p{N}])%iu';
$replace[$row['id']] = $row['replace_with'];
}
$this->model->searchList = $search;
$this->model->replaceList = $replace;
$result = $this->c->Cache->set('censorship', [
return [
'searchList' => $search,
'replaceList' => $replace,
]);
if (true !== $result) {
throw new RuntimeException('Unable to write value to cache - censorship');
}
return $this->model;
];
}
}

View file

@ -64,10 +64,6 @@ class Save extends Method
$this->c->DB->exec($query, $vars);
}
if (true !== $this->c->Cache->delete('censorship')) {
throw new RuntimeException('Unable to remove key from cache - censorship');
}
return $this->model;
return $this->model->reset();
}
}