добавлено кэширование класса Юзер

This commit is contained in:
rsgrinko committed 2022-04-18 17:41:14 +03:00
1 parent a4d65fa44d
commit 1412003d5e
2 files changed
+23 -9

No files matched your search

+10
View File
@@ -1,4 +1,10 @@
<?php <?php
/**
* Версия сервиса
*/
define('VERSION', '1.0.0');
/** /**
* Путь до корня проекта * Путь до корня проекта
*/ */
@@ -34,3 +40,7 @@
*/ */
define('USER_ONLINE_TIME', 60 * 5); define('USER_ONLINE_TIME', 60 * 5);
/**
* Время жизни кэша
*/
define('CACHE_TTL', 3600);
+13 -9
View File
@@ -55,6 +55,7 @@
if (empty($id)) { if (empty($id)) {
throw new CoreException('Передан некорректный идентификатор пользователя'); throw new CoreException('Передан некорректный идентификатор пользователя');
} }
$this->id = $id; $this->id = $id;
} }
@@ -65,8 +66,15 @@
*/ */
public function getAllUserData(): ?array public function getAllUserData(): ?array
{ {
$result = (DB::getInstance())->getItem(self::TABLE, ['id' => $this->id]); $cacheId = md5('User_getAllUserData_' . $this->id);
if ($result) { if(Cache::check($cacheId)) {
$result = Cache::get($cacheId);
} else {
$result = (DB::getInstance())->getItem(self::TABLE, ['id' => $this->id]);
Cache::set($cacheId, $result);
}
if (!empty($result)) {
return $result; return $result;
} else { } else {
return null; return null;
@@ -360,14 +368,10 @@
* *
* @return bool * @return bool
*/ */
public static function isAdmin(): bool public function isAdmin(): bool
{ {
if (self::isUser()) { if ($this->getAllUserData()['access_level'] == self::ACCESS_ADMIN) {
if (self::getFields(self::$id)['access_level'] == 'admin') { return true;
return true;
} else {
return false;
}
} else { } else {
return false; return false;
} }